Populating PostgreSQL database with data from a psql file

Hello there, my name is Abubakar and this is the devmaesters website. In this post I am going to be sharing with you a way to populate your postgreSql database with data from a psql file.

For the purposes of this post I am assuming that

  • You have set up postgreSQL database already in your local machine. 
  • And also that you have installed gitbash as well because that's the command line tool that i'll be using.
  • What am using is a windows os, so the commands might be a bit different for other operating systems.

Lets get right down to business then !!.

step 1

First of all navigate to the folder that contains the psql file and and right click on your mouse and from the options that appear click on git bash here to open the git bash command line interface in the curent directory.

Alernativel you can open the gitbash command line tool from the start menu and enter "ls" command to list all the available directories then navigate into the directory that contains your psql file using the "cd directory-name" command (note: if you make an error, you can navigate back to prevoius directory using the "cd .. " command).

step 2

note: skip this step if you have created your database already

Inside the gitbash window excute the following

psql -U postgres

What this does is to authenticate you with the default database and user credentials that gets created for you when you setup postgreSQL on your machine.

note:

If it requests for a password, just use the password for accessing pgAdmin software in your computer.

When you see this in your terminal it means that you have been authenticated sucessfully.

postgres=#

Next, create a new database executing the sql command below in your gitbash terminal

create database example;

From the command above, example is the name of the new database we want to create (remember it for we'll need it for step 3)

Execute the command below to quit your database and return to your psql file directory

\q

Step 3

Finally, execute the command below in your commandline terminal to populate the database specified by database-name with data from the psql file specified by file-name.psql as user speicified by your-username.

psql --username your-username --dbname database-name -f file-name.psql

When using the command above change the following:

  • Your-userame to the name of the user asociated with the database (In my example  in step 2, the username is postgres).
  • Database-name to the name of your database (the database i created in step 2 is named example).
  • Change file-name.psql to the actual name of the psql file in the current directory that you want to use to populate your database.

note:

If it requests for a password, just use the password for accessing pgAdmin software in your computer.

You can confirm that your database has been successfully poppuated by checking the database tables using the commandline by executing

psql -U postgres

When you see this in your terminal it means that you have been authenticated sucessfully.

postgres=#

With this you are now connected to the postgres database, switch to the database you just poppulated with data using

\c database-name

Where database-name is the name of your database.

And finally execute 

\d

To see the list of all the tables that are now in your database.

Conclusion

The method above is working fine on windows system using the gitbash commandline tool. As always thanks for reading, feel free to leave your comments and questions down below in the comment section

Author
author-image

Hello, my name is Abubakar Zakari. Am a budding fullstack developer from Nigeria who loves developing softwares and learning new frameworks and langauges.

Comment

Select image


Comments
No comment yet

DEVMAESTERS

Newsletter

Services

Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment

Contact

Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email: abubakarzakari1703@gmail.com

Copywright@devmaesters.com
Privacy Policy

By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.