back up and restore postgresql
Run a .sql file, dump a PostgreSQL database and load it back, from the terminal.
Steps
-
sudo -u postgres psqlconnect as the postgres admin ("role does not exist": use this)
-
psql -U NAME -d dbname -f file.sqlrun a .sql file
-
pg_dump -U NAME -d dbname -F c -f db.dumpdump a database in pg_restore's format
-
pg_restore -U NAME -d dbname db.dumpload it back
-
pg_dump -U NAME dbname > dump.sqla plain .sql dump, loaded with psql -f
-
psql -U NAME -c 'SELECT version();'which PostgreSQL this is
Notes
- A password without typing it: a ~/.pgpass file, or PGPASSWORD for one command.
sudo -u postgres createuser --interactivemakes a role for your user.
Commands used
psqlpg_dumppg_restore
Each has a page in lore: lore psql opens it.