How To Import Sql File In Postgresql In Windows
There is mainly two means to run whatever ".SQL" file in PostgreSQL, e.g., pgAdmin user interface and SQL shell of PostgreSQL. In this article, we will show you the implementation of running SQL files using both means for the ease of PostgreSQL users. Then, allow's showtime by using GUI offset.
Method 01: Via PgAdmin GUI
The get-go 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 x desktop and tap on it; when it is highlighted, then open it. It requires you to enter the Main password to run pgAdmin. After inbound the password, you may demand to add the PostgreSQL database countersign to become into it once again. Then, subsequently 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 beneath. Upon exploring, you can create a new database or easily use the already built-in Postgres database within the "Databases" option. And then, we volition apply the already built-in database Postgres from the list.
Now we have to open the query tool to run a ".sql" file. You tin tap on the query tool icon residing at the taskbar of pgAdmin. Another fashion to open up 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 incommunicable without a database. And then, right-click on the Postgres database and get a trivial down to the other options. You will detect an option of Query tool. Tap on the option of Query Tool, equally as well demonstrated in the image below, to open the query editor on your screen console 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 expanse on your pgAdmin screen can be seen in the output paradigm below. One can do coding here easily. We have to run a "SQL" file from our system; we have to outset import or add that file into the pgAdmin graphical user interface. For that purpose, you will be using the taskbar of query editor. Inside this taskbar, you will find an icon of the folder next to the icon of the database query tool icon and earlier the floppy bulldoze icon, which can be seen in the prototype below. Tap on that folder icon to explore and select the files and folders of your Windows x.
A window named "Select file" will be opened on your pgAdmin graphical user interface screen, as demonstrated in the image below. Yous tin can see, it volition open the default "C" directory of your Windows x automatically. Yous have to select the SQL file you want to import into the pgAdmin by choosing or changing the location of a file. Make certain your SQL file must contain some queries or command to exist executed. Add the location of a file to the search area.
Our SQL file has been located inside the "D" directory of Windows 10; nosotros have to navigate towards the intended location as per the beneath demonstrated image. After reaching the location, please tap on the SQL file to exist chosen and striking 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 every bit shown in the output prototype beneath. This code contains the table creation query to create a new tabular array customer with some columns related to the customer information. After that, some other query has been used to insert records in the table customer after creating it. At present, 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 tabular array has been created properly. The lawmaking inserted 9 records into the tabular array "customer" successfully within milliseconds as per the output message.
You can find out the newly created table "client" within the list of Tables held in the Postgres database as shown beneath in the image.
If you lot want to see that the code imported via sample.sql file has been fully functional, you accept to follow the procedure from at present on. We will also demonstrate how to cheque that the records take been successfully inserted within the shown tabular array afterwards the execution of the file. Become along with us to see the procedure that will exist 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 evidence all the records and data merely inserted in the pgAdmin Postgres database via the Sample.sql file. The output would be in the grid form, every bit demonstrated from the image. This was the simplest way to import an SQL file to the PostgreSQL database.
Method 02: Via SQL Beat
Another easiest and most used way to run any SQL file in PostgreSQL is via its SQL trounce. Open the SQL vanquish from the carte bar of Windows x. Add your server proper noun, database name where you desire to import the file, the port number you are currently active on, PostgreSQL username, and password to starting time using SQL vanquish. After adding all these requirements, you will work on the SQL shell, every bit shown in the output image below.
Make sure to save your SQL file into a directory that tin can be hands located. We haven't changed the location of our SQL file, e.one thousand., 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 accept to add the location of a file along with this special graphic symbol to run and import the file. So, we accept used the below query and hit Enter to execute it. The query worked fine, created a tabular array, and inserted 9 new records within the table as well.
# \i 'D:/Work/articles/Sample.sql'
When we used the SELECT query in the SQL shell to fetch the records, information technology displays the whole new tabular array "customer" along with its 9 records on our SQL screen.
# SELECT * FROM customer;
Conclusion:
Within this demonstration, nosotros accept 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.
Source: https://linuxhint.com/run-sql-file-postgresql/
Posted by: fryaraim1986.blogspot.com

0 Response to "How To Import Sql File In Postgresql In Windows"
Post a Comment