How to Import or Export CSVs to PostgreSQL Using pgAdmin

The graphical interface provides a variety of user-friendly features like easy to use, attractive interface, provides shortcuts, etc. Therefore, most beginners or intermediate users prefer graphical interfaces as compared to command-line interfaces. pgAdmin is the most popular GUI-based management tool to interact with the Postgres database. It allows us to import CSV files to Postgres tables or export Postgres tables to CSV files easily and efficiently.

This post will demonstrate how to import or export CSV files to PostgreSQL via pgAdmin.

Importing CSV Using pgAdmin

To import data from a CSV file to a Postgres table via pgAdmin, users need to follow the below-provided instructions.

Step 1: Sample CSV File

The below snippet shows the sample CSV file that we want to import into a Postgres table:

img

Step 2: Show Table’s Structure

Open the pgAdmin, provide the login privileges, and execute the following command to see the table’s structure:

SELECT * FROM staff_info;
img

Step 3: Import CSV File

To import a CSV file via pgAdmin, right-click on the table and click on the “Import/Export Data…” tab option:

img

In the “Import/Export Data Window”, select the “import” option, provide the “file name/path”, specify the file format, enable the “header” option, specify the delimiter, and hit the “OK” button to import the data from a CSV file to Postgres table:

img

Clicking on the “OK” button will show the following message:

img

Step 4: Verify the Exported Table’s Data

Let’s verify whether the data from CSV has been imported to the selected table or not:

SELECT * FROM staff_info;
img

The output shows that data from CSV has been imported to the “staff_info” table.

Exporting CSV Using pgAdmin

Users need to follow the below-provided instructions to export data from a Postgres table to a CSV file via pgAdmin.

Step 1: Show Table’s Data

Open the pgAdmin, provide the login privileges, and execute the following command to see the data from the selected table:

SELECT * FROM department_info;
img

Step 3: Export to CSV File

To export a CSV file via pgAdmin, right-click on the table and select the “Import/Export Data…” option:

img

In the “Import/Export Data Window”, select the “export” option, provide the “file name”, specify the file format, enable the “header” option, specify the delimiter, and hit the “OK” button to export the data from the Postgres table to CSV file:

img

Clicking on the “OK” button will show the following message window:

img

Step 4: Verify the Table’s Data

Let’s verify whether the data from the selected table has been exported to a CSV file or not. For this purpose, navigate to the directory/location where you exported the selected table:

img

A CSV file named “dept_data” has been exported to the specified location. Now open it to see its content:

img

The above snippet clarifies that the content from the “department_info” table has been successfully exported to a CSV file named “dept_data”.

Conclusion

PostgreSQL allows us to import CSV files to Postgres tables or export Postgres tables to CSV files using pgAdmin. To do that, right-click on a table of your choice > click on the “import/export data…” tab > select either the “import” or “export” option > provide the “file name” > specify the file format > enable the “header” option > specify the delimiter > hit the “OK” button to import or export CSVs to Postgres via pgAdmin. This post demonstrates stepwise instructions for importing CSV to Postgres or exporting Postgres tables to CSV files using pgAdmin.