Method 01: Via PgAdmin GUI
The first method to run a “.sql” file in PostgreSQL uses its pgAdmin graphical user interface. So, search for the installed pgAdmin GUI from the menu bar of the windows 10 desktop and tap on it; when it is highlighted, then open it. It requires you to enter the Master password to run pgAdmin. After entering the password, you may need to add the PostgreSQL database password to get into it once again. So, after adding the database password, tap on the “OK” button on a dialogue box opened on your screen to start using the pgAdmin graphical user interface.
The pgAdmin graphical interface opening screen has been shown below. Upon exploring, you can create a new database or easily use the already built-in Postgres database within the “Databases” option. So, we will use the already built-in database Postgres from the list.
Now we have to open the query tool to run a “.sql” file. You can tap on the query tool icon residing at the taskbar of pgAdmin. Another way to open the query tool is via the Database Postgres that has been built in already. We have to create a table or insert some records, which is impossible without a database. So, right-click on the Postgres database and go a little down to the other options. You will find an option of Query tool. Tap on the option of Query Tool, as also demonstrated in the image below, to open the query editor on your screen panel of the PostgreSQL pgAdmin graphical user interface.
This will open the Query Editor of pgAdmin within the database Postgres within the GUI window. The query area on your pgAdmin screen can be seen in the output image below. One can do coding here easily. We have to run a “SQL” file from our system; we have to first import or add that file into the pgAdmin graphical user interface. For that purpose, you will be using the taskbar of query editor. Within this taskbar, you will find an icon of the folder next to the icon of the database query tool icon and before the floppy drive icon, which can be seen in the image below. Tap on that folder icon to explore and select the files and folders of your Windows 10.
A window named “Select file” will be opened on your pgAdmin graphical user interface screen, as demonstrated in the image below. You can see, it will open the default “C” directory of your Windows 10 automatically. You have to select the SQL file you want to import into the pgAdmin by choosing or changing the location of a file. Make sure your SQL file must contain some queries or command to be executed. Add the location of a file to the search area.
Our SQL file has been located within the “D” directory of Windows 10; we have to navigate towards the intended location as per the below demonstrated image. After reaching the location, please tap on the SQL file to be chosen and hit the Select button on your screen to add it to the pgAdmin Postgres database query tool. We have selected the “Sample.sql” code file and tapped on the “Select” button to import it in the pgAdmin GUI.
Now the file sample.sql has been opened in pgAdmin. The code of sample.sql file has been imported in the query tool of pgAdmin as shown in the output image beneath. This code contains the table creation query to create a new table customer with some columns related to the customer information. After that, another query has been used to insert records in the table customer after creating it. Now, it’s time to execute this code file sample.sql through the query tool to run it. You have to tap on the triangular-shaped icon for “Run” to execute it for that purpose. So, tap on it to make this code “run”. The output shows that the code works fine and the table has been created properly. The code inserted 9 records into the table “customer” successfully within milliseconds as per the output message.
You can find out the newly created table “customer” within the list of Tables held in the Postgres database as shown beneath in the image.
If you want to see that the code imported via sample.sql file has been fully functional, you have to follow the procedure from now on. We will also demonstrate how to check that the records have been successfully inserted within the shown table after the execution of the file. Get along with us to see the procedure that will be demonstrated now. You have to right-click on the table “customer” that has just been created and click on the “View/Edit Data” option to fetch all the records on the pgAdmin graphical user interface, as demonstrated in the snapshot below.
It will show all the records and data just inserted in the pgAdmin Postgres database via the Sample.sql file. The output would be in the grid form, as demonstrated from the image. This was the simplest way to import an SQL file to the PostgreSQL database.
Method 02: Via SQL Shell
Another easiest and most used way to run any SQL file in PostgreSQL is via its SQL shell. Open the SQL shell from the menu bar of Windows 10. Add your server name, database name where you want to import the file, the port number you are currently active on, PostgreSQL username, and password to start using SQL shell. After adding all these requirements, you will work on the SQL shell, as shown in the output image below.
Make sure to save your SQL file into a directory that can be easily located. We haven’t changed the location of our SQL file, e.g., Sample.sql. So, the query used to run the SQL will be a slash sign with the alphabet “i” used to import the file. You have to add the location of a file along with this special character to run and import the file. So, we have used the below query and hit Enter to execute it. The query worked fine, created a table, and inserted 9 new records within the table as well.
When we used the SELECT query in the SQL shell to fetch the records, it displays the whole new table “customer” along with its 9 records on our SQL screen.
Within this demonstration, we have implemented two ways to import and run any SQL file to PostgreSQL pgAdmin user interface or SQL shell from the Windows 10 system. We hope this article will be helpful to those who are new to the PostgreSQL database.