How to Backup and Restore PostgreSQL Database (5 Tips)
Abstract: you have learned about backup and restore of PostgreSQL server databasePostgreSQL database server (also known as Postgres) is an advance database mana
PostgreSQL database server (also known as Postgres) is an advance database management system. The Postgres server provides psql
, pg_dump
and pg_dumpall
command line utilities to backup and restore databases.
This article will describe various ways to use of pg_dump command to backup database. Also you will learn how to restore postgres database backup via command line.
Backup and Restore Database in PostgresBelow is some connections options which you can use for connecting remote server or authenticated server with all queries given in this article.
-d, --dbname=DBNAME
database name-h, --host=HOSTNAME
database server hostname or ip-p, --port=PORT
database server port number (default: 5432)-U, --username=NAME
connect as specified database user-W, --password
force password prompt--role=ROLENAME
do SET ROLE before dump
- Backup a single database in PostgreSQL server. Use 「-d」 comamnd line option to provide database name to pg_dump command. Make sure to replace your actual database name in place of mydb.
pg_dump -h localhost -U postgres -W -d mydb > mydb.sql
- Restore a single database from backup in PostgreSQL. Just use 「psql」 command to restore PostgreSQL database.
psql -h localhost -U postgres -W -d mydb < mydb.sql
- Backup all databases in PostgreSQL using
pg_dumpall
utility.pg_dumpall -h localhost -U postgres -W > alldbs.sql
- Restore: all database backup using following command.
psql -h localhost -U postgres -W < alldbs.sql
- Backup: a single table named mytable from mydb database.
pg_dump -h localhost -U postgres -d mydb -W -t table_1 > mydb-table_1.sql
- Restore: single table backup to database. Make sure your backup file contains only single table backup which you want to restore.
psql -h localhost -U postgres -W -d mydb < mydb-table_1.sql
- Backup PostgreSQL database in compressed format. Use gzip command line utility to accept piped backup data and make archive file.
pg_dump -h localhost -U postgres -W -d mydb | gzip > mydb.sql.gz
- Restore database from compressed backup file directly. Here gunzip will extract backup file and send data to psql command via pipe interface.
gunzip -c mydb.sql.gz | psql -h localhost -U postgres -W -d mydb
- Backup: PostgreSQL database and split backup in multiple files of specified size. It helps us to backup a large database and transfer to other host easily. As per below example it will split backup files of 100mb in size.
pg_dump -h localhost -U postgres -W -d mydb | split -b 100m – mydb.pql
- Restore: database backup from multiple splited backup files.
cat mydb.sql* | psql -h localhost -U postgres -W -d mydb
- Backup: database in compressed splited files of specified size.
pg_dump -h localhost -U postgres -W -d mydb | gzip | split -b 100m – mydb.sql.gz
- Restore: database from multiple files of compressed files.
cat mydb.sql.gz* | gunzip | psql -h localhost -U postgres -W -d mydb
In this tutorial, you have learned about backup and restore of PostgreSQL server database, tables.