Site Tools


quickref:postgres

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
quickref:postgres [2025-11-04 Tue 14:16] – created theorytoequickref:postgres [2026-11-19 Thu 03:33] (current) – demon-machine
Line 7: Line 7:
 CREATE DATABASE <dbname>; CREATE DATABASE <dbname>;
 CREATE ROLE <rolename>; CREATE ROLE <rolename>;
-ALTER USER backend WITH PASSWORD '<password>'; +ALTER USER <rolename> WITH PASSWORD '<password>'; 
-ALTER USER backend WITH LOGIN;+ALTER USER <rolename> WITH LOGIN;
 ALTER DATABASE <dbname> OWNER TO <rolename>; ALTER DATABASE <dbname> OWNER TO <rolename>;
 GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <rolename>; GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <rolename>;
Line 23: Line 23:
 UPDATE t SET idcolumn=nextval('seq'); UPDATE t SET idcolumn=nextval('seq');
 </code> </code>
 +
 +===== Add/Drop Constraint =====
 +Postgres names constraints on table creation in the following format: ''(table name)_(column name)_(constraint type)''.
 +<code sql>
 +ALTER TABLE <table> ADD CONSTRAINT <constraint_name> <constraint_expression>;
 +ALTER TABLE <table> DROP CONSTRAINT <constraint_name>;
 +</code>
 +
 +===== Backup / Restore =====
 +<code>
 +# Dump full db
 +pg_dump -U <user> -h <host> -d <db> > backup.sql
 +
 +# Table, schema, or data only
 +pg_dump -U <user> -d <db> -t <table> > table.sql
 +pg_dump -U <user> -d <db> -s > schema.sql
 +pg_dump -U <user> -d <db> -a > data.sql
 +
 +# Custom format (restore with pg_restore)
 +pg_dump -U <user> -d <db> -Fc > backup.dump
 +
 +# Restore
 +psql -U <user> -d <db> < backup.sql
 +pg_restore -U <user> -d <db> backup.dump
 +</code>
 +
quickref/postgres.1762265793.txt.gz · Last modified: by theorytoe