×


psql to restore a text dump How to do it

Are you trying to use psql to restore a text dump?

This guide is for you.


A database dump also known as SQL dump contains a record of the table structure and/or the data from a database and is usually in the form of a list of SQL statements. A database dump is most often used for backing up a database so that its contents can be restored in the event of data loss.

Using psql is one of the simplest ways to restore a text dump. Text dumping is a suitable importing method for small databases.


Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to restore text dumps.

In this context, we shall look into how to restore a text dump using psql (PostgreSQL).


Steps to use psql to restore a text dump ?

The dump method generates a SQL file of a database. Restoring this file recreates the database in the previous state.

Before going to the steps of restoring let's see how to take a text dump file of a database.


Here we will use  pg_dump  to export a PostgreSQL database.

i. First, access the database directly or remotely to dump it. Now, dump the database using the following command.

pg_dump -U db_user -W -F p db_name > /path_of_the_dump/dump_file.sql

Here, the db_user is the database user and db_name is the database name. The other options are,

-U denotes a user.

-W prompt for a password before connecting to the server.

F denotes file format.


ii. To dump the database in a text file format, use the option p.


How to Restore a text dump?

Restoring a text dump file is only possible using psql. This is an interactive interface for managing the PostgreSQL database.

So,  for restoring the text dump we can take the psql console and run the following command:

psql -U db_user db_name < dump_file.sql

This restores the dump file successfully.


Common errors while using psql to restore a text dump ?

Sometimes the psql text dump restoring can end up in errors. Now, let’s see a few possible errors that occur during a text file restoration. We will also see how our Support Team fix these errors for our customers.


1. Invalid command while restoring sql

Sometimes, the psql dump restores ends up in the following error message.

psql:psit.sql:27485: invalid command \N

Here the \N denotes a null value. The error message itself is not much informative. In such situations, we will try to debug the error.


psql has an inbuilt mode ‘stop on the first error‘. So, we will switch psql to ‘stop on the first error mode’. This displays the initial trigger for the error. To switch the mode, we will use the following command:

psql -v ON_ERROR_STOP=1

This command gives the actual reason for the error which will help us to fix it.


2. Improper restoring of the dump file

At times we will encounter the following error message.


pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

This error message is seen when the user tries to restore a text-based dump file using the pg_restore tool.


Usually, the pg_dump tool creates a dump file in text format. A single step restoration of a text file is only possible by using psql.

In order to use pg_restore, we must convert the text file to tar format and then execute the restore command.

So, to fix the error, we will restore the dump file in the appropriate format.


[Still, having trouble in restoring a text dump using psql? – We will help you.]


Conclusion

This article will guide you on steps to perform  text dump restoration. Basically, psql restores a text dump in the same file format.

You can backup a single table, a #schema, or a complete database. Select the name of the backup source in the #pgAdmin tree control, right click to open the context menu, and select Backup… to open the #Backup dialog. 

The name of the object selected will appear in the dialog title bar.

In #PostgreSQL, you can restore a database in two ways:

i. Using psql to restore plain #SQL script file generated by pg_dump and pg_dumpall tools.

ii. Using pg_restore to restore tar file and directory format created by the pg_dump tool.


To Restore Postgres Database:

1. Restore a postgres database. $ psql -U erp -d erp_devel -f mydb.sql.

2. Backup a local postgres database and restore to remote server using single command: $ pg_dump dbname | psql -h hostname dbname.

3. Restore all the postgres databases.

4. Restore a single postgres table.