tag (postgresql)


reset auto increment counter in postgres Jun

postgres uses something called a sequence to keep track of the auto increment counts. To list the sequences us:


The sequence name is in the format of ${table}_{variable}_seq

You can reset the counter using the function

setval('product_id_seq', 1453);


select setval('product_id_seq', (SELECT MAX(id) FROM product));

If you do not know the name you can use the pg_get_serial_sequence function on the table 

select pg_get_serial_sequence('product', 'id');

You can also manually update the sequence with the command:

ALTER SEQUENCE product_id_seq RESTART WITH 1453;


how to rename a database in postgresql Oct


First kick the users out of the database, you can find the users with the following command.

    FROM pg_stat_activity 
    WHERE datname = 'myolddbname_goes_here'

now run the following:

ALTER DATABASE myolddbname_here RENAME TO mynewdbname_here


export a table into a csv file in postgresql Oct

I always have to look this up, so I'm placing this here. This exports a table into a csv file in PostgreSQL.

COPY (SELECT * FROM table) TO `/path/to/file.csv` WITH CSV HEADER;


postgresql cheatsheet Jun

I'm transitioning my databases from MySQL to PostgreSQL. Overall it's been an easy transition. One of the main differences that I had to adjust to is that by default postgres uses the unix user accounts to handle authentication into the CLI. This can be changed in the settings, and it also means that you might want to set the password for the postgres unix user.

Also it's good to note that postgres automatically translates non-strings to lowercase. This is an issue if you do things like camel casing your tables and columns, so it's best to avoid doing so if you're into this habit.

I do find the command lines to be more streamlined and the config to be more straight forward, but that's really just a personal preference.

Here's a rundown of the commands I often use. I'm posting them here for my convenience, but perhaps someone else might also find these useful.

# login
psql -U <user>
psql -U <user> -d <database>
psql -U <user> -h <host> -d <database>

# user management
CREATE USER <user> WITH PASSWORD '<password>'
ALTER USER <user> WITH PASSWORD '<password>'
DROP USER <user>

createuser -D -A -P <user>
psql -U <admin> -c "CREATE USER <user> WITH PASSWORD '<password>'" -d template1

# database
CREATE DATABASE <database> OWNER <user>
DROP DATABASE <database>

ALTER TABLE <table> OWNER TO <user>

createdb -O <user> <database>

# install
apt-get install posgresql
apt-get install python-psycopg2


apt-get install libpq-dev
pip install psycopg2

# configuration
# pg_hba.conf
host all all md5 # open up all connections, used for dev

# postgresql.conf
listen_addresses = '*' # listen for addresses

# dump and load data
pg_dump <database> > <file.sql>
psql <database> < <file.sql>

# from http://developer.postgresql.org/pgdocs/postgres/app-pgdump.html
To dump a database called mydb into a SQL-script file:

$ pg_dump mydb > db.sql

To reload such a script into a (freshly created) database named newdb:

$ psql -d newdb -f db.sql

To dump a database into a custom-format archive file:

$ pg_dump -Fc mydb > db.dump

To reload an archive file into a (freshly created) database named newdb:

$ pg_restore -d newdb db.dump

To dump a single table named mytab:

$ pg_dump -t mytab mydb > db.sql

To dump all tables whose names start with emp in the detroit schema,
except for the table named employee_log:

$ pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb > db.sql

To dump all schemas whose names start with east or west and end in gsm,
excluding any schemas whose names contain the word test:

$ pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb > db.sql

The same, using regular expression notation to consolidate the switches:

$ pg_dump -n '(east|west)*gsm' -N '*test*' mydb > db.sql

To dump all database objects except for tables whose names begin with ts_:

$ pg_dump -T 'ts_*' mydb > db.sql

To specify an upper-case or mixed-case name in -t and related switches,
you need to double-quote the name; else it will be folded to lower case (see Patterns).
But double quotes are special to the shell, so in turn they must be quoted.
Thus, to dump a single table with a mixed-case name, you need something like

$ pg_dump -t '"MixedCaseName"' mydb > mytab.sql

# export csv
psql -U <user> <database>
\f ','
\o file.csv
SELECT <statement>