Tech Tips: How do I Backup and Restore the Postgres Database?

Karan Singh

19 Jun 2013

Email

1 min read

Tech Tips: How do I Backup and Restore the Postgres Database?

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

    • COPY (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.sql

    This 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:

Backup a Postgres Table:

$ /opt/msys/3rdParty/bin/pg_dump–table maincontrol.orgs -U ecuser pe -f ms_table.sql

The above command is an example of how to backup a specific table from a Postgres database. Using the above command we are backing up table ‘orgs’ in schema ‘maincontrol’  from database ‘pe’ to ms_table.sql file. To backup a specific table, use the –table TABLENAME option in the pg_dump command. If there are same table names in different schema then use the –schema SCHEMANAME option.

  1. Backup a Specific Postgres database :

    $ /opt/msys/3rdParty/bin/pg_dump -U ecuser pe -f pe_dump.sql

    This is an example of backing up a specific Postgres database. Using the above command we are backing up the database for Message Central ‘pe’ to file pe_dump.sql. The backup file has create table, alter table and copy commands for all the tables in the ‘pe’ database.

  2. Backup all Postgres Databases :

    $ /opt/msys/3rdParty/bin/pg_dumpall -U ecuser> all_dump.sql

    You can backup all the databases using pg_dumpall command. The above command will create a dump of all the databases that reside on the Postgres instance running on a particular server. To list all the database that have been backed up use the command grep “^[\]connect” all.sql.

Backup a Postgres Table:

$ /opt/msys/3rdParty/bin/pg_dump–table maincontrol.orgs -U ecuser pe -f ms_table.sql

The above command is an example of how to backup a specific table from a Postgres database. Using the above command we are backing up table ‘orgs’ in schema ‘maincontrol’  from database ‘pe’ to ms_table.sql file. To backup a specific table, use the –table TABLENAME option in the pg_dump command. If there are same table names in different schema then use the –schema SCHEMANAME option.

  1. Backup a Specific Postgres database :

    $ /opt/msys/3rdParty/bin/pg_dump -U ecuser pe -f pe_dump.sql

    This is an example of backing up a specific Postgres database. Using the above command we are backing up the database for Message Central ‘pe’ to file pe_dump.sql. The backup file has create table, alter table and copy commands for all the tables in the ‘pe’ database.

  2. Backup all Postgres Databases :

    $ /opt/msys/3rdParty/bin/pg_dumpall -U ecuser> all_dump.sql

    You can backup all the databases using pg_dumpall command. The above command will create a dump of all the databases that reside on the Postgres instance running on a particular server. To list all the database that have been backed up use the command grep “^[\]connect” all.sql.

Backup a Postgres Table:

$ /opt/msys/3rdParty/bin/pg_dump–table maincontrol.orgs -U ecuser pe -f ms_table.sql

The above command is an example of how to backup a specific table from a Postgres database. Using the above command we are backing up table ‘orgs’ in schema ‘maincontrol’  from database ‘pe’ to ms_table.sql file. To backup a specific table, use the –table TABLENAME option in the pg_dump command. If there are same table names in different schema then use the –schema SCHEMANAME option.

  1. Backup a Specific Postgres database :

    $ /opt/msys/3rdParty/bin/pg_dump -U ecuser pe -f pe_dump.sql

    This is an example of backing up a specific Postgres database. Using the above command we are backing up the database for Message Central ‘pe’ to file pe_dump.sql. The backup file has create table, alter table and copy commands for all the tables in the ‘pe’ database.

  2. Backup all Postgres Databases :

    $ /opt/msys/3rdParty/bin/pg_dumpall -U ecuser> all_dump.sql

    You can backup all the databases using pg_dumpall command. The above command will create a dump of all the databases that reside on the Postgres instance running on a particular server. To list all the database that have been backed up use the command grep “^[\]connect” all.sql.

How to Restore Postgres Database

  1. Restore a Postgres table :

    $ /opt/msys/3rdParty/bin/psql -U ecuser -f ms_table.sql pe

    The 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.

  2. Restore a postgres Database :

    $ /opt/msys/3rdParty/bin/psql -U ecuser -d pe -f pe_dump.sql

    Similar 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.

  3. Restore all Databases :

    $ /opt/msys/3rdParty/bin/psql -u ecuser -f all_dump.sql

    Restore 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.

Other news

Read more from this category

A person is standing at a desk while typing on a laptop.

The complete AI-native platform that scales with your business.

© 2025 Bird

A person is standing at a desk while typing on a laptop.

The complete AI-native platform that scales with your business.

© 2025 Bird