How to load a Postgres Database

postgres

Today, we will learn about how to load a Postgres database using pgadmin and CLI.

You can download a sample database from here:
Sample Database

Using Pgadmin

Create a database

Go to pgadmin –> Databases –> Create –> Database

After clicking this, you will see a mini-window like this, name your database and pick the owner (by default Postgres)

Hit SAVE and the database will be created.

Load database from source file

Load the file into the database by using the restore option provided in pgadmin

After clicking Restore, this window will open.

Select the filename by clicking on the folder/directory icon and select the tar file.

If you can’t find the tar file, select all files instead of custom files when looking for a tar file.

click Restore and the database will be loaded successfully, like this

Using CLI

Open the psql CLI

psql

after that, enter your details like this:

Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:

Create a Database

postgres=# CREATE DATABASE testdb;
CREATE DATABASE

The database has been created and you can now exit the psql tool using.

postgres=# exit

Navigate to your folder where the database dump is present using the ‘cd’ command or directly opening the terminal in that directory.

Enter the below command to load the file into the PostgreSQL database.

Use pg_restore to load

pg_restore -U postgres -d testdb /path/to/file.tar

You will be prompted to enter the password, enter that and we are good to go.

You can verify that the database has been created and the data has been loaded by going to PgAdmin or querying into psql tool (use \l to list the databases).

Explore more resources about programming from –> here.

Software Engineer | Website | + posts

Talha is a seasoned Software Engineer with a passion for exploring the ever-evolving world of technology. With a strong foundation in Python and expertise in web development, web scraping, and machine learning, he loves to unravel the intricacies of the digital landscape. Talha loves to write content on this platform for sharing insights, tutorials, and updates on coding, development, and the latest tech trends

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *