Import data from CSV
This topic describes how to import data into a Neon database table from a CSV file.
The instructions require a working installation of psql. The psql client is the native command-line client for PostgreSQL. It provides an interactive session for sending commands to PostgreSQL. For more information about psql, refer to the psql reference, in the PostgreSQL Documentation.
The following example uses the default neondb database, a table named customer, and a data file named customer.csv. Data is loaded from the customer.csv file into the customer table.
-
Connect to the
neondbdatabase usingpsql. For example:psql postgres://casey:<password>@ep-polished-water-579720.us-east-2.aws.neon.tech/neondbnote
For more information about connecting to Neon with
psql, see Connect with psql. -
Create the
customertable.CREATE TABLE customer ( id SERIAL, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(255), PRIMARY KEY (id) )tip
You can also create tables using the SQL Editor in the Neon Console. See Query with Neon's SQL Editor.
-
Prepare a
customer.csvfile with the following data:First Name,Last Name,Email 1,Casey,Smith,casey.smith@example.com 2,Sally,Jones,sally.jones@example.com -
From your
psqlprompt, load the data from thecustomer.csvfile using the\copyoption.\copy customer FROM '/path/to/customer.csv' DELIMITER ',' CSV HEADERIf the command runs successfully, it returns the number of records copied to the database:
COPY 2For more information about the
\copyoption, refer to the psql reference, in the PostgreSQL Documentation.
Need help?
Send a request to support@neon.tech, or join the Neon community forum.