PostgreSQL – Loading a Database

In this article we will look into the process of loading a PostgreSQL database into the PostgreSQL database server.
Before moving forward we just need to make sure of two things:

  • PostgreSQL database server is installed on your system.
  • A sample database.

For the purpose of this article, we will be using a sample database which is DVD rental database.
You can download the sample dvdrental database from here.

The Sample Database:

So, the DVD rental database that we will be using ahead in the article represents a DVD rental store. The objects in the database includes:

  • 15 tables
  • 1 trigger
  • 8 functions
  • 1 domain
  • 7 views
  • 13 sequences

ER Model of the sample Database:

Tables in the Sample Database:
There are 15 tables in our sample database which are listed below:



  1. actor – stores actors data including first name and last name.
  2. film – stores films data such as title, release year, length, rating, etc
  3. film_actor – stores the relationships between films and actors.
  4. category – stores film’s categories data.
  5. film_category– stores the relationships between films and categories.
  6. store – contains the store data including manager staff and address.
  7. inventory – stores inventory data.
  8. rental – stores rental data.
  9. payment – stores customer’s payments.
  10. staff – stores staff data.
  11. customer – stores customers data.
  12. address – stores address data for staff and customers
  13. city – stores the city names.
  14. country – stores the country names.

So now we know everything about our sample DVD rental database, let us move on to loading the same database to the PostgreSQL database server. The steps to which are listed below:

  • Step 1: Create a DVD rental Database, by opening the SQL shell. Once you open up the shell, you will need to add the necessary credentials for your database, which would somewhat look like below:
    Server [localhost]:
    Database [postgres]:
    Port [5432]:
    Username [postgres]:
    Password for user postgres:

    Now using the CREATE DATABASE statement create a new database as follows:

    CREATE DATABASE dvdrental;
  • Step 2: Load the databse file by creating a folder at your desired location(say, c:\users\sample_database\dvdrental.tar). Now open up command prompt and Navigate to the bin folder of the PostgreSQL installation folder as below:

    Use the pg_restore tool to load data into the dvdrental database that we had just created as using the command:

    pg_restore -U postgres -d dvdrental C:\users\sample_datbase\dvdrental.tar

    It would somewhat look like below:

    Now enter your database user Password and your sample database will be loaded.

Verify Database Load:

Now if you need to verify if the sample database is loaded, use the below command to get into the database in SQL shell:

\c

Now to list all the tables in the database, use the below command:

\dt

The result should look like below:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.