The Wayback Machine - https://web.archive.org/web/20240926135143/https://www.geeksforgeeks.org/postgresql-loading-a-database/
Open In App

PostgreSQL – Loading a Database

Last Updated : 20 Sep, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 database 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:  

 


Previous Article
Next Article

Similar Reads

PostgreSQL - Connect To PostgreSQL Database Server in Python
The psycopg database adapter is used to connect with PostgreSQL database server through python. Installing psycopg: First, use the following command line from the terminal: pip install psycopg If you have downloaded the source package into your computer, you can use the setup.py as follows: python setup.py build sudo python setup.py installCreate a
4 min read
PostgreSQL - Export PostgreSQL Table to CSV file
In this article we will discuss the process of exporting a PostgreSQL Table to a CSV file. Here we will see how to export on the server and also on the client machine. For Server-Side Export: Use the below syntax to copy a PostgreSQL table from the server itself: Syntax: COPY Table_Name TO 'Path/filename.csv' CSV HEADER; Note: If you have permissio
2 min read
PostgreSQL - Installing PostgreSQL Without Admin Rights on Windows
If you are a part of a corporation, it is highly unlikely that you have the admin privileges to install any external software. But the curious souls that all software developers are, in this article, we will see the detailed process of installation of PostgreSQL without having administrator rights on our Windows machine. Installation: Follow the be
3 min read
PostgreSQL - Rename Database
In PostgreSQL, the ALTER DATABASE RENAME TO statement is used to rename a database. The below steps need to be followed while renaming a database: Disconnect from the database that you want to rename by connecting to a different database.Terminate all connections, connected to the database to be renamed.Now you can use the ALTER DATABASE statement
1 min read
PostgreSQL - Connecting to the database using Python
PostgreSQL API for Python allows user to interact with the PostgreSQL database using the psycopg2 module. In this article we will look into the process of connecting to a PostgreSQL database using Python. Prerequisites: First we will need to install the psycopg2 module with the below command in the command prompt: pip install psycopg2 Creating a Da
2 min read
How to use PostgreSQL Database in Django?
This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are choosing a database for your applications. Also ch
2 min read
PostgreSQL - Connect and Access a Database
In this article, we will learn about how to access the PostgreSQL database. Once the database is created in PostgreSQL, we can access it in two ways using: psql: PostgreSQL interactive terminal program, which allows us to interactively enter, edit, and execute SQL commands.pgAdmin: A graphical frontend web-based tool, suite with ODBC or JDBC suppor
3 min read
How To Connect and run SQL queries to a PostgreSQL database from Python
This article focus on connecting to a PostgreSQL database from Python. Installation:Install PostgreSQL, If you haven't installed it.We need to install the psycopg2 library to connect to a PostgreSQL database. Open the command prompt and run the below command to install psycopg2pip3 install psycopg2Creating a Database You can create a Database in 2
2 min read
How to Export PostgreSQL Database Without Data Using SQL?
When we are working with the PostgreSQL database, there are multiple times we need to export the database structure. This approach is useful when we create a skeleton database or migrate the schema changes for different environments or systems. In this article, we will explore the process of exporting a database without data using SQL with the help
3 min read
How to Implement PostgreSQL Database in Rails Application?
In this article, we are going to look into the implementation of PostgreSQL in a rails application. As we know that database is a very important part of any web application that's why today modern web applications like Flipkart, Amazon, Netflix all the websites are use database. Before going forward we need to know a little about what is the databa
2 min read
How to Migrate a MySQL Database to PostgreSQL using pgloader?
Database migration is a common task in software development when switching between different database management systems (DBMS). In this article, we'll explore how to migrate a MySQL database to PostgreSQL using a powerful tool called pgloader. We'll cover the concepts involved, and the steps required, and provide detailed examples with outputs to
6 min read
How to Migrate a PostgreSQL Database to MySQL
Moving a database from one platform to another can be tough, but with careful planning and execution, it can be done smoothly. In this article, we'll go over how to migrate a PostgreSQL database to MySQL, which are both popular RDBMS. We'll cover preparation, schema conversion, data migration, and testing, using detailed examples and outputs to hel
5 min read
How to Dump and Restore PostgreSQL Database?
PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases between environments the necessity of knowledge on how
5 min read
PostgreSQL - Copy Database
Copying databases can be essential for testing, backup, or migration purposes. PostgreSQL provides several methods to accomplish this efficiently. Here, we will discuss the process of copying a PostgreSQL database on the same server or from one server to another. Copying a PostgreSQL Database Within the Same ServerSometimes, you may need to create
3 min read
PostgreSQL - Create Database
PostgreSQL provides multiple ways to create a database, catering to different user preferences and requirements. This article will discuss various methods to create a database in PostgreSQL, including using the psql shell and pgAdmin. Creating a Database Using psql ShellTo create a database through the psql shell, you can use the CREATE DATABASE st
3 min read
PostgreSQL ALTER DATABASE
The PostgreSQL ALTER DATABASE statement is a powerful tool used for modifying an existing database.  The features of a database, once created can be changed using the ALTER DATABASE statement.  Let's explore the syntax of PostgreSQL ALTER DATABASE, the various actions that can be performed, and practical examples for efficiently managing your Postg
3 min read
PostgreSQL - Restore Database
Restoring a PostgreSQL database involves copying backed-up data and restoring it to its original or a new location. This process is crucial for returning data to its pre-loss, pre-corruption state, ensuring data integrity and availability. Common reasons for database restoration include human errors, malicious attacks, power outages, and natural di
4 min read
PostgreSQL - Size of a Database
In PostgreSQL, managing storage and understanding database size is critical for performance monitoring and resource management. PostgreSQL offers built-in functions that allow you to evaluate the size of databases, helping you manage storage more effectively. One of the most commonly used functions for this purpose is the 'pg_database_size()' funct
2 min read
PostgreSQL - Backup Database
All commercial firms and corporations are never 100% free of data threats. Being wary of scenarios like data corruption, host or network failures or even deliberate storage facility destruction is extremely important. Therefore, backing up data is a critical activity that every firm depends on to ensure its existence in the market. The idea behind
12 min read
How to Select the Nth Row in a PostgreSQL Database Table?
In PostgreSQL, selecting specific rows is a fundamental operation frequently required for tasks such as data analysis, pagination, and reporting. The "nth" row refers to the row in a table that holds a particular position or rank, where "n" represents that specific position or ranking number. This article explores various techniques to efficiently
5 min read
PostgreSQL - DROP DATABASE
When managing databases in PostgreSQL, you may need to delete an existing database that is no longer required. The DROP DATABASE statement is designed for this purpose. It permanently removes the specified database, along with all of its catalog entries and data directories. This action is irreversible, so it must be executed with caution. Only the
3 min read
Finding the Best AI for PostgreSQL Database
Artificial Intelligence (AI) is changing many industries, including database management. GPT models, a type of AI are improving how PostgreSQL databases are managed. They help automate tasks like writing SQL queries, optimizing performance, and predicting potential database problems. In this article, we'll explore how AI-driven GPT models can enhan
6 min read
PostgreSQL - Creating Updatable Views Using WITH CHECK OPTION Clause
PostgreSQL is the most advanced general purpose open source database in the world. pgAdmin is the most popular management tool or development platform for PostgreSQL. It is also an open source development platform. It can be used in any Operating Systems and can be run either as a desktop application or as a web in your browser. In this article, we
4 min read
What is PostgreSQL - Introduction
This is an introductory article for the PostgreSQL database management system. In this we will look into the features of PostgreSQL and why it stands out among other relational database management systems. Brief History of PostgreSQL: PostgreSQL also known as Postgres, was developed by Michael Stonebraker of the University of California, Berkley. I
2 min read
Install PostgreSQL on Mac
This is a step-by-step guide to install PostgreSQL on a Mac OS machine. We will be installing PostgreSQL version 11.3 on Mac using the installer provided by EnterpriseDB in this article. There are three crucial steps for the installation of PostgreSQL as follows: Download PostgreSQL EnterpriseDB installer for MacInstall PostgreSQLVerify the install
2 min read
PostgreSQL - NOT LIKE operator
The PostgreSQL NOT LIKE works exactly opposite to that of the LIKE operator. It is used to data using pattern matching techniques that explicitly excludes the mentioned pattern from the query result set.Its result include strings that are case-sensitive and doesn't follow the mentioned pattern. It is important to know that PostgreSQL provides with
2 min read
PostgreSQL - LIKE operator
The PostgreSQL LIKE operator is used query data using pattern matching techniques. Its result include strings that are case-sensitive and follow the mentioned pattern. It is important to know that PostgreSQL provides with 2 special wildcard characters for the purpose of patterns matching as below: Percent ( %) for matching any sequence of character
2 min read
PostgreSQL - SELF JOIN
PostgreSQL has a special type of join called the SELF JOIN which is used to join a table with itself. It comes in handy when comparing the column of rows within the same table. As, using the same table name for comparison is not allowed in PostgreSQL, we use aliases to set different names of the same table during self-join. It is also important to
3 min read
Install PostgreSQL on Linux
This is a step-by-step guide to install PostgreSQL on a Linux machine. By default, PostgreSQL is available in all Ubuntu versions as PostgreSql "Snapshot". However other versions of the same can be downloaded through the PostgreSQL apt repository. We will be installing PostgreSQL version 11.3 on Ubuntu in this article. There are three crucial steps
2 min read
PostgreSQL - Size of tablespace
In this article, we will look into the function that is used to get the size of the PostgreSQL database tablespace. The pg_tablespace_size() function is used to get the size of a tablespace of a table. This function accepts a tablespace name and returns the size in bytes. Syntax: select pg_tablespace_size('tablespace_name'); Example 1: Here we will
2 min read