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
createdb
command to create a new database. (Read more about the createdb command).
user$ createdb -hlocalhost -p 5432 -U postgress testdb
createdb
is the name of the command you are executing.-h
specifies the host, which should be the localhost for default installs.-p
specifies the port to connect through, which should be 5432 for default installs.-U
specifies the username you are connecting as, which should be postgres.testdb
is 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_restore
command 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_restore
is the name of the command you are executing.-v
option 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.-h
option is the host, as increatedb
-p
option is the port, as increatedb
-U
option is the user, as increatedb
-d
option is the database to restore to, which should be the name of the database you created in step 1 with thecreatedb
command~/Desktop/Database\ Downloads/dumptest.dmp
is 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_restore
command. 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.
Comments
0 comments
Please sign in to leave a comment.