====== PosgreSQL QuickRef ====== PostgreSQL quick reference. ===== Create Role & DB ===== CREATE DATABASE ; CREATE ROLE ; ALTER USER WITH PASSWORD ''; ALTER USER WITH LOGIN; ALTER DATABASE OWNER TO ; GRANT ALL PRIVILEGES ON DATABASE TO ; ===== Reset Serial Count ===== -- Reset sequence ALTER SEQUENCE seq RESTART WITH 1; -- update column to reference sequence -- (not needed if the column in question is already -- referenced by the sequence) UPDATE t SET idcolumn=nextval('seq'); ===== Add/Drop Constraint ===== Postgres names constraints on table creation in the following format: ''(table name)_(column name)_(constraint type)''. ALTER TABLE ADD CONSTRAINT ; ALTER TABLE
DROP CONSTRAINT ; ===== Backup / Restore ===== # Dump full db pg_dump -U -h -d > backup.sql # Table, schema, or data only pg_dump -U -d -t
> table.sql pg_dump -U -d -s > schema.sql pg_dump -U -d -a > data.sql # Custom format (restore with pg_restore) pg_dump -U -d -Fc > backup.dump # Restore psql -U -d < backup.sql pg_restore -U -d backup.dump