Tech Tips: How do I Backup and Restore the Postgres Database?
Karan Singh
19 Jun 2013
1 min read

Key Takeaways
PostgreSQL backups can target a single table, a single database, or all databases depending on your needs.
pg_dumpcreates a SQL text file containing DDL and data needed to recreate the target table or database.pg_dumpallbacks 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, andCOPYcommands 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_dumpgenerates 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 TABLENAMEto export a specific table. If tables with the same name exist across schemas, use--schemato 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_dumpallbacks 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 TABLEALTER TABLECOPY(for table data)
bringing the database back 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. You can take a backup locally and restore it to a remote Postgres instance, provided the versions are compatible.
Does pg_dump affect the running database?
No. It works via SQL queries and does not lock the entire database. It safely captures a consistent snapshot.
How do I restore all databases from a pg_dumpall file?
Use:
psql -U <user> -f all_dump.sqlThis recreates every database found in the dump.
First, a disclaimer. This post provides a general guide on how to backup a Postgres Database Table. The examples provided do not refer to any specific table or database. If you are trying to backup a specific table, you should also backup tables which refer to the original tables. As Postgres is a relational database, there will always be references between a number of tables.
This is a ‘how to’ doc which only gives example commands, how it is done actually is solely dependent on the person doing it and specific use cases. Always maintain the integrity of the data you are trying to backup. Backing up the entire database is always a safe option, but when backing up specific tables one needs to be careful.
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:
How to Restore Postgres Database
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. The above command will give us all the Postgres databases in the exact state that they were in when a dump was taken from the original database server.



