Overview
Once you have your DB download, you need to restore it in order to use it and access any information contained within. This article will show you how to restore it via CLI.
Process
- Open a new terminal/cmd window and use the
createdbcommand to create a new database. (Read more about the createdb command).
user$ createdb -hlocalhost -p 5432 -U postgress testdb
createdbis the name of the command you are executing.-hspecifies the host, which should be the localhost for default installs.-pspecifies the port to connect through, which should be 5432 for default installs.-Uspecifies the username you are connecting as, which should be postgres.testdbis the name chosen for the new database. You may want to replace it with a different name.
- You will be prompted for a password, which is the password of the postgres user (not your OS user!). This should be the password you created when installing postgres. Once you enter this password, the database should be created successfully and you should get a new terminal line with no output, so your terminal window should look like this:
user$ createdb -h localhost -p 5432 -U postgress testdb
Password:
user$ - Use the
pg_restorecommand to restore the dump file to the newly created database. (Read more about the pg_restore command).
user$ pg_restore -v -h localhost -p 5432 -U postgres -b testdb ~/Desktop/Database/Downloads/dumptest.dmp
pg_restoreis the name of the command you are executing.-voption indicates verbose mode. This means the program will print information about what it is doing as it is running. This is optional but can be helpful if you want to troubleshoot issues.-hoption is the host, as increatedb-poption is the port, as increatedb-Uoption is the user, as increatedb-doption is the database to restore to, which should be the name of the database you created in step 1 with thecreatedbcommand-
~/Desktop/Database\ Downloads/dumptest.dmpis the path to the dump file.
Note: If you wish to restore the backup of a single table from the database backup, you can add the-t <tablename>option to thepg_restorecommand. E.g.pg_restore -v -h localhost -U postgres -d testdb -t <Table_Name> ~/Desktop/Database/Downloads/dumptest.dmp
- Again, you will be prompted for the password of the postgres user. Once you enter it, the restore will execute. If you did not include the verbose option, when the command finishes you will get a new terminal line with no output, such as below:
user$ pg_restore -h localhost -p 5432 -U postgres -d testdb ~/Desktop/Database\ Downloads/dumptest.dmp
If you did include the verbose option, then you will see extra messaging preceded by
Password:
user$pg_restore:, while the command is running. You will know that it is done when a new terminal line is presented.
Confirmation
The database has been restored and it is ready to be used.
Priyanka Bhotika
Comments