PostgreSQL - LOAD DATABASE



In PostgreSQL, loading a database is used to restore the data from the backup or external sources. In this tutorial, we will learn the different methods to load a database.

Different ways to load database in PostgreSQL

Following is the list of methods to load the database using different methods. −

Using restore

Suppose you have a backup in custom format like .dump or .backup, then use the pg_restore command.

Syntax

Following is the syntax of loading a database using the restore command −

pg_restore -U postgres -d target_database backup_file.dump

Here,

  • -U postgres specifies the PostgreSQL user.
  • -d target_database is the database where data will be restored.
  • backup_file.dump is the backup file.

Using psql

To back up the database in plain SQL format, use the psql command to restore it.

psql -U postgres -d target_database -f backup_file.sql

Here,

  • -f backup_file.sql specifies the SQL file to execute.

Importing Data from CSV files

If you want to load the database with external sources, use the COPY command.

COPY table_name FROM '/path/to/file.csv' DELIMITER ',' CSV HEADER;

Here,

  • /path/to/file.csv − This is file location.
  • DELIMITER ',' specifies that values are comma-separated.
  • CSV HEADER indicates that the first row contains column names.

Using pgAdmin

pgAdmin is a free opens−source tool that helps users to manage PostgreSQL and other relational databases. Below are some steps that help you to load the database −

Step 1 : Open the pgAdmin and connect with PostgreSQL server.

Step 2 : Then select the target database.

Step 3 : Go to Tools −> restore.

Step 4 : Next, choose the file for backup and configure the restore setting.

Step 5 : Finally, click the restore to load the database.

Therefore, all of these methods are effective ways to load data into a database using PostgreSQL.

Advertisements