Introduction to PostgreSQL Backup
If you are using the PostgreSQL database in the production environment for your clients then you need to make sure that your database is always working fine and is available 24 * 7. For the availability of the database, you must often keep the backup of your client’s database. In case if the database is corrupted or is crashed or lost then you should be able to restore the data in the database. For this reason, PostgreSQL provides us with a facility to keep the backups using three methods –
Methods of PostgreSQL Backup
Three methods of PostgreSQL backup are given below:
1. SQL Dump
There is a facility to dump the database using pg_dump utility. The advantage of this utility is that it is not server-side version specific and can work even on the machine having different architectures. For example, if you dump your database from a 32-bit architecture machine and then use it in a 64-bit machine then it will work fine.
2. File System-Level Backup
As we all know the data of the PostgreSQL database is stored in files and directory structure. The idea behind file system level backup is to store the backup of these files and then restore them whenever required. But it has many disadvantages such as the database system needs to be closed i.e server should be shut down while taking the backup. This type of backup methodology can only be used when you have to take the complete backup and restoration of the database in its entirety. The file data can be stored in any of the formats you wish. One of the techniques is –
tar -cf mydatabasebackup.tar /etc/psql/12/data
that exports the data in the tar file on the specified location – /etc/psql/12/data
3. Continuous Archiving
This methodology combines the File system-level backup methodology and Write Ahead Logs (WAL) that are stored in pg_xlog. The logs are maintained for eav=ch and every transaction that is being performed on the database. The checkpoint is also maintained to know whether the transaction is successfully committed and completed. Whenever a crash occurs or data is lost. It can be recovered by restoring the data stored in the file-based system and then the logs from that point are run up to checkpoint to bring the database to the current state. However, this process is a bit clumsy and complex but provides some benefits than the other two as discussed previously.
The most convenient and preferred method out of them is SQL dump which will be discussed in brief in the upcoming session.
Explanation of SQL Dump
You must dump your database frequently to have the updated backup of your database available to you. Whenever the backup is restored the database will be back to the state when that dump file was being created using pg_dump. One more advantage of using the pg_dump utility instead of file-level backups and archiving is that it is not specific to the version of the PostgreSQL server.
Also, the database operations which are running while pg_dump is not locked and run in the background except some of the operations related to structural changes such as ALTER TABLE are blocked. You will have to specify external -o in the pg_dump command in case your database schema depends on OIDs such as foreign keys. For this, you will also need to dump the OIDs.
Syntax:
pg_dump databaseName > outputFile
- pg_dump: It is the utility program that is provided in PostgreSQL to store the current state of the database into a file that will contain commands which when run on the database server will recreate the state of the database when it was dumped using the same. It is a client-side program and needs to be run through the Linux command line prompt.
- databaseName: It is the name of the database which you want to dump.
- outputFile: It is the name of the file which has to be created after the dumping is finished.
pg_dump process needs to be done only by the superuser login as it has permission to read all the databases and tables. Although, you do have a facility to dump the database remotely. By default the environment variables PGHOST, PGPORT, and PGUSER are considered as the localhost,5432, and username using which you default to connect to database which is the same as the username of the operating system.
You can change the PGHOST and PGPORT by using the options -h for host and -p for the port in pg_dump command. To change and override the PGUSER variable you can use the -U option in the statement. All this is confirmed using the client authentication mechanisms.
Example:
pg_dump postgres > myBackupFile
Check whether the file is created successfully. Let us use ls command which lists all the files in the current directory –
ls
Now, our backup file which is dumped is ready with name myBackupFile.
Let us see the contents of the Postgres database by checking all its tables with the help of \dt command.
\c postgres
\dt
So, it contains three tables which we will drop one by one. By using the commands
drop table educba;
drop table teams cascade;
drop table developers;
and then further check whether all of them are deleted by \dt command.
\dt
So, now the database Postgres does not contain any tables in it. Now, we will learn how we can restore the data into the database.
Restoring the Dump
Now, if we have a backup file, we can anytime restore the database in case if we want to and the database is corrupted or lost. However, you need to consider some things before restoring. The database which you are trying to restore should be present on the database server. If not then you should create the database with the help of the command
- createdb -T template0 Postgres
in our case. As the database already exists which can be checked by using the command \l which results in follow by the database server, I don’t need to execute the createdb command now.
\l
The other thing is to make sure that the database which you are restoring should already consist of all the users who had right on it. In our case, the Postgres user is the owner of the Postgres database. Hence, or database server should already have a user named Postgres or else it will give an error while restoring. You can check this by firing the command –
SELECT usename FROM pg_catalog.pg_user;
which results in the following –
Now, the Postgres database and Postgres user are already present in my database server. I can go for restoring the database using the command –
psql postgres < myBackupFile;
as the syntax of restoring the dump is
psql empty_database < backup_file
where empty_database is the database that you want to restore and backup_file is the file which was the resultant of the pg_dump command.
psql postgres < myBackupFile;
gives the output as follows.
Now, if we check the contents of the Postgres database using the command \dt it gives the output as –
\dt
Also, the contents of all the tables are restored correctly –
select * from developers;
Handling Restoration Errors
PostgreSQL continues to restore the database even when any error is occurred in between. It only tells about them at the end of the completion of the restore. In this case, the database is restored incompletely. If you want to stop the restoration process on error occurrence, you can use –
psql --set ON_ERROR_STOP=on postgres < myBackupFile
ON_ERROR_STOP option helps you to stop the restoration process whenever the error occurs. Now, if you want to perform restoration in the transactional manner i.e either the whole database should be restored or nothing then you can use -1 or –single-transaction option to make it a transactional process.
psql -1 postgres < myBackupFile
For dumping all the databases in the current database server to a file you can use pg_dumpall utility program and provide the default database while restoration.
Conclusion – PostgreSQL Backup
We can dump our database, so that backup is available in the near future to restore the database in case of any problem with data available from the database using pg_dump in PostgreSQL. Other methods such as File system level backup and Continuous archiving can also be used to keep the backup of your PostgreSQL database.
Recommended Articles
This is a guide to PostgreSQL Backup. Here we also discuss the introduction and three methods of PostgreSQL backup along with examples and its code implementation. You may also have a look at the following articles to learn more –