Introduction to Postgres Dump Database
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 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.
You must dump your database frequently to have the updated backup of your database available with 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_sump are 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 if 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 -U option in the statement. All this is confirmed using the client authentication mechanisms.
For example
pg_dump postgres > myBackupFile
ls
Check whether the file is created successfully. Let us use ls command which lists all the files in the current directory –
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;
\dt
and then further check whether all of them are deleted by \dt command.
So, now my 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 check by using the command \l which results in follow in by database server, we don’t need to execute the createdb command now.
\l
The other thing 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, as 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 –
Also, the contents of all the tables are restored correctly –
select * from developers;
Handling restoration errors
The PostgreSQL continues to restore the database even when any error is occurred in between. It only tells about them at the end of completion of 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 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 default database while restoration.
Conclusion: Postgres Dump Database
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.
Recommended Articles
This is a guide to Postgres Dump Database. Here we discuss the introduction of Postgres Dump Database, syntax, parameters, and examples with code implementation. You may also have a look at the following articles to learn more –