I had a problem with PostgreSQL pgdump recently. My setval() calls were all set to '1'.
I wrote this script to fix things:
#!/usr/bin/env python
DB_NAME = 'my_db'
from subprocess import Popen, PIPE
import re
exclude = [ 'tablename', 'rows' ]
tp = re.compile( '[^a-z_]' )
ts = Popen( [ "/usr/bin/psql", DB_NAME, "-c SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg_%' AND tablename NOT LIKE 'sql_%' ORDER BY tablename" ], stdout=PIPE ).communicate()[ 0 ].split( ' ' )
tables = []
for t in ts:
t = tp.sub( '', t )
if len( t ) == 0 or t in exclude:
continue
tables.append( t )
for t in tables:
sql = "SELECT pg_catalog.setval( pg_get_serial_sequence( '%s', 'id' ), ( SELECT MAX( id ) FROM %s ) + 1 );" % ( t, t )
print Popen( [ "/usr/bin/psql", DB_NAME, "-c %s" % sql ], stdout=PIPE ).communicate()[ 0 ]
Page Updates |
---|
Cisco CCNA Study :: Home Lab Notes |
PostgreSQL Backup Script |
Generate new Factorio map |
Install and setup snmpd on RedHat Enterprise Linux |
Install and setup irssi and oidentd on Debian |
Recent Articles |
---|
Install xfce4 on Debian |
Console Blackjack in Perl |
Selenium::WebDriver::Error::UnknownError |
Game of Life in C++ using the SDL2 |
PigPen (dots and boxes) in C++ using the SDL2 |
Copyright © 2005 - 2021
GregDonald.com · Contact · EUGOR · Nautical War · CRM12
All Rights Reserved