Backup
Backup – SQL Dump
pg_dump [options] [dbname]
pg_dump Options
Backup:
(Backup data to a file)
pg_dumpall [options...] > filename.backup
Recovery:
(Recover data from a file)
Any database in the cluster can be used for the initial connection – it doesn’t have to be template1
- As with any database, PostgreSQL database should be backed up regularly.
- There are three fundamentally differentapproaches to backing up PostgreSQL data:
- SQL dump
- File system level backup
- On-line backup
Backup – SQL Dump
- Generate a text file with SQL commands
- PostgreSQL provides the utility program pg_dump for this purpose.
- pg_dump does not block reads or writers.
- pg_dump does not operate with special permissions. In particular, it must have read access to all tables that you want to back up, so in practice you almost always have to run it as a database superuser.
- Dumps created by pg_dump are internally consistent, that is, the dump represents a snapshot of the database as of the time pg_dump begins running.
pg_dump [options] [dbname]
pg_dump Options
- -a – Data only. Do not dump the data definitions (schema)
- -s – Data definitions (schema) only. Do not dump the data
- -n
- Dump from the specified schema only
- -t - Dump specified table only
- -f
- Send dump to specified file
- -Fp – Dump in plain-text SQL script (default)
- -Ft – Dump in tar format
- -Fc – Dump in compressed, custom format
- -v – Verbose option
- -o use oids
Restore – SQL Dump
- The text files created by pg_dump are intended to be read in by the psql program. The general commandform to restore a dump is– psql dbname < infile where infile is what you used as outfile for the pg_dump command. The database dbname will not be created by this command, so you must create it yourself.
- pg_restore is used to restore a database backed up with pg_dump that was saved in an archive format – i.e., a non-text format Files are portable across architectures
Syntax:
pg_restore [options...] [filename.backup]
pg_restore [options...] [filename.backup]
pg_restore Options
- -d
- Connect to the specified database. Also restores to this database if –C option is omittedselect pg_start_backup('label_goes_here')
- -C – Create the database named in the dump file & restore directly into it
- -a – Restore the data only, not the data definitions (schema)
- -s – Restore the data definitions (schema) only, not the data
- -n
- Restore only objects from specified schema
- -t - Restore only specified table
- -v – Verbose option
Backup:
(Backup data to a file)
- pg_dumpall is used to dump an entire database cluster in plain-text SQL format
- Dumps global objects - user, groups, and associated permissions
- Use PSQL to restore
pg_dumpall [options...] > filename.backup
pg_dumpall Options
- -a – Data only. Do not dump the data definitions(schema)
- -s - Data definitions (schema) only. Do not dump the data
- -g - Dump global objects only – i.e., users and groups
- -v – Verbose option
Recovery:
(Recover data from a file)
Syntax:
psql –d template1 < filename.backup
or
psql –d template1 –f filename.backup
psql –d template1 < filename.backup
or
psql –d template1 –f filename.backup
Any database in the cluster can be used for the initial connection – it doesn’t have to be template1
Backup - File system level backup
- An alternative backup strategy is to directly copy the files that PostgreSQL uses to store the data in the database.
- You can use whatever method you prefer for doing usual file system backups, for example:
- tar -cf backup.tar /usr/local/pgsql/data
- The database server must be shut down in order to get a usable backup.
- File system backups only work for complete backup and restoration of an entire database cluster.
Backup - On-line backup
- Use when database must stay up while backup is occurring.
- postgres=# select pg_start_backup('label_goes_here')
- Copy the files/directory
- postgres=# select pg_stop_backup();
- Archive_command must be set in postgresql.conf which archives WAL logs and supports PITR