Tech Tips: How do I Backup and Restore the Postgres Database?. This 'how-to' document provides example commands, but the actual implementation depends on the individual and specific use cases.

Key Takeaways
- PostgreSQL backups can target a single table, a single database, or all databases depending on your needs.
- pg_dump creates a SQL text file containing DDL and data needed to recreate the target table or database.
- pg_dumpall backs up every database on the Postgres instance.
- Restoring is done with psql, which executes the SQL dump back into the target database.
- Table-level restores require ensuring the table does not already exist to avoid conflicts.
- When backing up individual tables, be mindful of relational dependencies.
- Dumps can be taken from local and restored to remote Postgres instances.
- Backups include CREATE, ALTER, and COPY commands depending on what is being exported.
- Proper backup strategy depends on understanding table relationships, schema usage, and deployment environments.
- Always ensure data integrity by taking backups that include referenced tables when necessary.
Q&A Highlights
What does pg_dump actually do? pg_dump generates a text-based SQL file containing the full set of commands needed to recreate a table or database exactly as it existed at the time of backup.
Can I back up just one table instead of a full database? Yes. Use pg_dump --table TABLENAME to export a specific table. If tables with the same name exist across schemas, use --schema to specify the correct one.
Should I worry about related tables when backing up a single table? Absolutely. PostgreSQL is relational, so other tables might reference the one you're backing up. If you restore only one table without related ones, you may break referential integrity.
How do I back up a full database? Run a command like: pg_dump -U <user> <database> -f <output.sql>. This exports all tables, schema definitions, indexes, and data for that database.
What does pg_dumpall do? pg_dumpall backs up every database on the server into a single SQL file. It's useful when migrating or snapshotting an entire Postgres instance.
Can I restore a table into an existing database? Yes. Use psql -U <user> -f <file.sql> <database>. But ensure the table does not already exist — otherwise you'll get "already exists" errors during restore.
What happens during a full database restore? The restore file runs CREATE TABLE, ALTER TABLE, and COPY commands, returning the database to its exact state at dump time.
Can I restore to a different server than the one I backed up from? Yes. Dumps are portable across compatible Postgres versions.
Does pg_dump affect the running database? No. It does not lock the entire database and safely captures consistent snapshots.
How do I restore all databases from a pg_dumpall file?
Use: psql -U <user> -f all_dump.sql
Introduction
This guide provides example commands, though actual implementation depends on individual use cases. When backing up specific tables, also backup tables with referential relationships. As a relational database, Postgres tables maintain references between one another. This is a how-to document with example commands only; implementation depends on the user and specific circumstances. Always maintain data integrity during backups.
SQL-dump/pg_dump:
The idea behind the SQL-dump method is to generate a text file with SQL commands that, when fed back to the server, will recreate the database in the same state as it was in at the time of the dump. PostgreSQL provides the utility program pg_dump for this purpose.
pg_dump is an effective and comprehensive tool to take Postgres database backups and use the backups to restore the postgres database. It is however not restricted to Database only. We can use pg_dump to backup tables and then use that to restore individual tables as well. Using pg_dump you can backup the local database and restore it on a remote database.
How to Backup Postgres Database:
Postgres Backup Options Overview
| Backup scope | Tool used | Output file | What it includes | When to use |
|---|---|---|---|---|
| Single table | pg_dump | .sql | Table DDL + data | Isolated table recovery or migration |
| Single database | pg_dump | .sql | All tables, schema, data | Full database backup |
| All databases | pg_dumpall | .sql | Every database on the instance | Server-wide backup or migration |
Backup a Postgres Table:
$ /opt/msys/3rdParty/bin/pg_dump–table maincontrol.orgs -U ecuser pe -f ms_table.sql
This example backs up the 'orgs' table in schema 'maincontrol' from database 'pe' to ms_table.sql. Use the –table TABLENAME option for specific tables. Use –schema SCHEMANAME when same table names exist across schemas.
Backup a Specific Postgres Database:
$ /opt/msys/3rdParty/bin/pg_dump -U ecuser pe -f pe_dump.sql
This backs up database 'pe' to pe_dump.sql. The backup file contains create table, alter table, and copy commands for all tables.
Backup all Postgres Databases:
$ /opt/msys/3rdParty/bin/pg_dumpall -U ecuser> all_dump.sql
pg_dumpall creates a dump of all databases on the Postgres instance. List backed-up databases using: grep "^\[\\\]connect" all.sql.
How to Restore Postgres Database
Postgres Restore Options Overview
| Restore scope | Tool | Input | Prerequisite | Risk |
|---|---|---|---|---|
| Single table | psql | .sql | Table must not already exist | "already exists" errors |
| Single database | psql | Database dump .sql | Target DB must exist | Overwriting unintended data |
| All databases | psql | Full dump .sql | Superuser access recommended | Restoring more than intended |
- Restore a Postgres table:
$ /opt/msys/3rdParty/bin/psql -U ecuser -f ms_table.sql peThe above command will install the table that was backed up in ms_table.sql file to the 'pe' database. Make sure that this table does not already exist, or you will see a bunch of 'already exists' errors. This command creates the table and exports all the data to the newly created table. - Restore a Postgres Database:
$ /opt/msys/3rdParty/bin/psql -U ecuser -d pe -f pe_dump.sqlSimilar to restoring the table, we can use the above command to restore the complete database. Here we are restoring the 'pe' database using the file pe_dump.sql which we had created while backing up the database in the Backup section above. - Restore all Databases:
$ /opt/msys/3rdParty/bin/psql -u ecuser -f all_dump.sqlRestore all the databases using the above command. 'all_dump.sql' was the file that was created using pg_dumpall.