PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Administration / Import CSV File Into PosgreSQL Table

Import CSV File Into PosgreSQL Table

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

We will create a new table named persons with the following columns:

  •  id: the person id
  •  first_name: the first name of the person
  •  last_name: the last name of the person
  •  dob date of birth of the person
  •  email: the email address of the person

We execute the following CREATE TABLE statement to create the persons table:

1
2
3
4
5
6
7
8
9
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)
)

posgresql import csv

We also prepare a CSV data file by using Microsoft Excel. You can use any applications that available in your PC to do so.

csv data

csv file

The path of the CSV file is as follows: C:\tmp\persons.csv

Import CSV file into table using COPY statement

To import this CSV file into the persons table, you use COPY statement as follows:

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

PosgreSQL gives back the following message:

posgresql import csv result

Let’s check the persons table.

1
SELECT * FROM persons;

posgresql import csv persons table

It works.

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

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

1
2
COPY sample_table
FROM 'C:\tmp\sample_data.csv' DELIMITER ',' CSV HEADER;

Second, you put the CSV file path after the FROM keyword. Because CSV file format is used, you need to specify DELIMITER as well as CSV keywords.

Third, HEADER keyword indicates that the CSV file contains a header line with column names. When importing data, PostgreSQL ignores the first line, which is the header line of the file.

Notice that the file must be read directly by the PosgreSQL server, not by the client application. Therefore, it must be accessible to the PosgreSQL server machine. Also you can execute COPY statement successfully if you have superusers access.

Import CSV file into table using pgAdmin III

In case you don’t have superuser privilege or you need to import CSV file from a client machine into a table in PosgreSQL server, you can use pgAdmin GUI tool to do that.

Let’s remove all data of the persons table so that we can re-import data and see the effect.

1
TRUNCATE TABLE persons;

First, right click on the persons table, choose the import… menu item as follows:

PostgreSQL import csv from pgAdmin GUI tool 1

Then, browse the the CSV file, choose format as CSV and click the Columns tab.

PostgreSQL import csv from pgAdmin GUI tool 2

Next, uncheck the id column because we don’t import data into this column and click the Misc. Options

PostgreSQL import csv from pgAdmin GUI tool 3

After that, check the Header checkbox because our CSV file has a header, choose comma (,) as the delimiter, and click Import button.

PostgreSQL import csv from pgAdmin GUI tool 4

Finally, when the import process completes, click the Done button.

PostgreSQL import csv from pgAdmin GUI tool 5

Let’s check the persons table again to see if the data is really imported.

posgresql import csv from pgAdmin tool persons table

Yes, it is.

In this tutorial, we have shown you how to import data from CSV file into a table using COPY statement as well as the pgAdmin tool.

Previous Tutorial: PostgreSQL Restore Database
Next Tutorial: Export PostgreSQL Table To CSV File

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.