A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql-import-csv-file-into-table/ below:

PostgreSQL - Import CSV File Into Table

PostgreSQL - Import CSV File Into Table

Last Updated : 15 Jul, 2025

Importing data from a CSV (Comma-Separated Values) file into a PostgreSQL database is a common task for database administrators and developers. PostgreSQL provides the COPY command, which is a powerful and efficient way to bulk load data directly from a CSV file into a table.

In this article, we will discuss the process of importing a '.csv' file into a PostgreSQL table.

Steps to Import CSV Data into PostgreSQL

Let us look at the steps of Importing CSV Data into a PostgreSQL Table.

Step 1: Creating a Target Table in PostgreSQL

To do so we will require a table which can be obtained using the below command:

CREATE TABLE persons
(
id serial NOT NULL,
first_name character varying(50),
last_name character varying(50),
dob DATE,
email character varying(255),
CONSTRAINT persons_pkey PRIMARY KEY (id)
);

This table will store personal information such as 'first_name', 'last_name', 'dob' (date of birth), and 'email'.

Step 2: Preparing the CSV File

Now we create a '.csv' file in our sheet manager (eg: MS Excel or notepad) as shown below. The structure of your CSV file should match the columns in the 'persons' table.

Our file is located as 'persons.csv' at 'C:\Users\Raju'.

Step 3: Importing the CSV File into PostgreSQL

To import the CSV file into the 'persons' table, you can use the 'COPY' statement. This statement reads the data from the CSV file and inserts it into the specified table.

COPY persons(first_name, last_name, dob, email) 
FROM 'C:\Users\Raju' DELIMITER ', ' CSV HEADER;
Step 4: Verifying the Data Import

After running the 'COPY' statement, you can verify that the data has been imported successfully by querying the 'persons' table:

SELECT * FROM persons;

It will lead to the below Output:

Important Considerations When Importing CSV Files into PostgreSQL


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4