EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Import CSV
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL Import CSV

By Priya PedamkarPriya Pedamkar

PostgreSQL Import CSV

Introduction to PostgreSQL Import CSV

PostgreSQL import CSV is defined as load the data into the table by using the CSV file, we have used comma-separated file (CSV) to import the data from the file into the PostgreSQL table. To import the data from the CSV file into the table, the same table needs to be present on the database also we need the same structure of the table in which data was present in the CSV file. Import data from CSV file is very useful and important operations in PostgreSQL for importing data from the file. We have to use the copy command to import data from CSV file.

Syntax and Parameters

Below is the syntax of the import CSV file into the PostgreSQL table.

COPY table_name (Column_name1, column_name2, …, column_nameN) from file_name (Path of file) Delimiter [ Character of delimiter which was we have used ] CSV [Header of CSV file.]

COPY name_of_table from name_of_file Delimiter [ Character of delimiter which was we have used ]

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is the parameter description syntax of import CSV into the PostgreSQL table.

  • Copy: This is a command in PostgreSQL used to import the data from the CSV file into the table. The copy command is very useful to import the data into the PostgreSQL table.
  • Table name: The table name specifies the name of table on which we have imported the data from CSV file.
  • Column name1 to column names: We have to define column name with the copy command. The column name specifies that we have to insert or load the data from the CSV file on which column that we have specify in copy command.
  • From: This keyword is used to specify the CSV file name from which we have importing the data into the table.
  • Filename or path of file: This is specify that name of file from which we have imported data into the table. It is defined as the path of the file, we need to define the absolute path with copy command in PostgreSQL.
  • Delimiter: This is defined as the value which was separated by a semicolon. We can use any delimiter to separate the value of a column in PostgreSQL.
  • Header of CSV file: This is defined as the column name of the table on which we have imported data from the CSV file. Data have their header to defined or import the data from file.

How Import CSV work in PostgreSQL?

Below is the working of import CSV file into the table in PostgreSQL. We have using copy command in PostgreSQL to import the file. To import the data from CSV file into the table we need to follow below things or same table is present on database.

1. Create table with same structure of CSV file

To import the data from CSV file into the table, same table is created with same name and same structure. Number and data type of column need to be same and which was present into the CSV file.Mismatch of column is not allowed while importing data from CSV file. If column name and data is mismatch then it will issue error as “ERROR: extra data after last expected column”. Below is the example that we required same name of column and data while importing data into the table.

Code:

copy import_test (id) from 'https://cdn.educba.com/Test.csv' DELIMITER ',' CSV HEADER;
[root@test2 /]# cat /Test.csv

Output:

PostgreSQL Import CSV - 1

2. Determine the delimiter

Basically we have separate the values using a comma, but in some file, we have used another delimiter to separate the values. We can also separate the values using ‘|’, tabs ‘\t’ or newline ‘\n’. Tab-separated values in PostgreSQL is also known as the TSV file. The delimiter is very important while importing data from CSV file into the table.

3. Check the data have header or not

  • Some CSV file contains the header or some file is not contains the header. The header is nothing but the column name which was we have defined in table.
  • The header is defined in the first line of the CSV file. If the CSV file contains the header then we need to define the header in copy command.
  • There is no need to define header when the header is not present in the CSV file.

Below example shows that we need to define header when CSV file contains the header.

Code:

COPY stud_test FROM 'https://cdn.educba.com/stud_test.csv' DELIMITER ',' CSV HEADER;
COPY stud_test FROM 'https://cdn.educba.com/stud_test.csv' DELIMITER ',';
cat /stud_test.csv

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

Output:

PostgreSQL Import CSV - 2

Examples to Implement Import CSV in PostgreSQL

Below are the examples mentioned:

We have using stud_test table to describe example of import CSV file into PostgreSQL table.

Below is the structure of stud_test table.

Code:

CREATE TABLE stud_test( id serial NOT NULL, f_name character varying(10), l_name character varying(10), dob date, email character varying(20));
\d+ stud_test;

Output:

PostgreSQL Import CSV - 3

1. Import CSV file into table by using header

In the below example, we have import CSV file into the table by using header. We have used the stud_test table to import the data from the CSV file.

Code:

COPY stud_test FROM 'https://cdn.educba.com/stud_test.csv' DELIMITER ',' CSV HEADER;
select * from stud_test;
cat /stud_test.csv

Output:

using header

2. Import CSV file into a table without using header

In the below example we have import CSV file into the table without using header. We have used the stud_test table to import the data from the CSV file.

Code:

COPY stud_test FROM 'https://cdn.educba.com/stud_test.csv' DELIMITER ',';
select * from stud_test;
cat /stud_test.csv

Output:

using header

3. Import CSV file into a table from shell prompt

In the below example,  we have import CSV file into the table by using shell prompt.

Code:

psql -d postgres -U postgres -d testing -c "COPY stud_test FROM 'https://cdn.educba.com/stud_test.csv' DELIMITER ',';"
select * from stud_test;

Output:

shell prompt

Conclusion

We import the data from the CSV file into the table, to import the data from the CSV file is faster than the normal insert. We have import the file with and without specifying the header. Also, we can use the column name with a copy command to import the data from CSV file.

Recommended Articles

This is a guide to PostgreSQL Import CSV. Here we discuss an introduction to PostgreSQL Import CSV, syntax, hoe does it work with query examples. You can also go through our other related articles to learn more –

  1. PostgreSQL Recursive Query
  2. PostgreSQL Administration
  3. PostgreSQL Primary Key
  4. Cursors in PostgreSQL
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more