Movatterモバイル変換


[0]ホーム

URL:


You don’t need a vector database - just use Postgres for everything. Read the case study on switching from Pinecone to Neon
PostgreSQL Tutorial
PostgreSQL Tutorial
/Getting Started/Import CSV File into Table

Import CSV File Into PostgreSQL Table

Summary: in this tutorial, we will show you various ways to import a CSV file into a PostgreSQL table.

First,create a new table namedpersons with the following columns:

  • id: the person id
  • first_name: first name
  • last_name: last name
  • dob date of birth
  • email: the email address
CREATE TABLE persons (  id SERIAL,  first_name VARCHAR(50),  last_name VARCHAR(50),  dob DATE,  email VARCHAR(255),  PRIMARY KEY (id));

posgresql import csvSecond, prepare a CSV data file with the following format:

csv datacsv fileThe path of the CSV file is as follows:C:\sampledb\persons.csv

Download the persons.csv file

Import a CSV file into a table using COPY statement

To import this CSV file into thepersons table, you useCOPY statement as follows:

COPY persons(first_name, last_name, dob, email)FROM 'C:\sampledb\persons.csv'DELIMITER','CSV HEADER;

PostgreSQL gives back the following message:

COPY 2

It means that two rows have been copied. Let’s check thepersons table.

SELECT * FROM persons;

It works as expected.

Let’s dive into the COPY statement in more detail.

First, you specify the table with column names after theCOPY keyword. The order of the columns must be the same as the ones in the CSV file. In case the CSV file contains all columns of the table, you don’t need to specify them explicitly, for example:

COPY sample_table_nameFROM 'C:\sampledb\sample_data.csv'DELIMITER','CSV HEADER;

Second, you put the CSV file path after theFROM keyword. Because CSV file format is used, you need to specifyDELIMITER as well asCSV clauses.

Third, specify theHEADER keyword to indicate that the CSV file contains a header. When theCOPY command imports data, it ignores the header of the file.

Notice that the file must be read directly by the PostgreSQL server, not by the client application. Therefore, it must be accessible by the PostgreSQL server machine. Also, you need to have superuser access to execute theCOPY statement successfully.

Import CSV file into a table using pgAdmin

In case you need to import a CSV file from your computer into a table on the PostgreSQL database server, you can use the pgAdmin.

The following statementtruncates thepersons table so that you can re-import the data.

TRUNCATE TABLE personsRESTART IDENTITY;

First, right-click thepersons table and select theImport/Export… menu item:

Second, (1) switch to import, (2) browse to the import file, (3) select the format as CSV, (4) select the delimiter as comma (,):

Third, click the columns tab, uncheck the id column, and click the OK button:

Finally, wait for the import process to complete. The following shows the dialog that informs you of the progress of the import:

In this tutorial, you have learned how to import data from a CSV file into a table on the PostgreSQL database server using theCOPY statement and pgAdmin tool.

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp