Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 September Import or Export a CSV File using PostgreSQL COPY Command

Import or Export a CSV File using PostgreSQL COPY Command

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on how to export or import CSV data using the PostgreSQL COPY command.

The CSV files are most famous for the necessary data migration activities. Database developer does not need to write or create any particular database link between two servers.

They can easily import data in CSV and export that CSV into another server and also CSV file does not have any data length limitation like any other XLS file.

User can store millions of records into CSV file.

This technique is preferred only for temporary purposes, and if it continues the process, the user has to write some automated process like database server replication.

PostgreSQL provides the COPY command to import or export CSV data into the PostgreSQL table.

Let me demonstrate this:

First, I create one table and import data from CSV file. I have already created a CSV file with sample data. Make sure that, your table column and CSV column would be same.

You can create this CSV file using a comma delimited save as option of Microsoft excel.

Postgres CSV

1
2
3
4
5
CREATE TABLE tbl_testcsv
(
rno integer,
name character varying
);

Now COPY the data from CSV file into the table:

1
2
3
COPY tbl_testcsv FROM 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;

After executing of COPY command, you can check the result:

1
SELECT *FROM tbl_testcsv;

Postgres CSV Tablel Data

Add some more records in Table and then we export the data into CSV file:

1
2
3
4
INSERT INTO tbl_testcsv
VALUES
(9,'YRQ'),(10,'QWE')
,(11,'MNB'),(12,'FGH');

Now, export this data using the COPY command:

1
2
3
4
COPY tbl_testcsv TO 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;
--Query returned successfully: 12 rows affected,

Now, check your CSV file. You can find newly inserted data:

You can also define a particular column or write a full select statement in the COPY command:
Below is a sample script.

1
2
3
4
COPY tbl_testcsv(rno)
TO 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;

1
2
3
4
COPY (SELECT rno,name FROM tbl_testcsv WHERE rno=8)
TO 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;

This is a full detailed example of Postgres COPY command, please test this sample and let me know for further assistance.

Sep 20, 2015Anvesh Patel
Delete all duplicate rows in MySQLString Array as an input parameter in PostgreSQL
Anvesh Patel
Anvesh Patel

Database Engineer

September 20, 2015 PostgreSQLAnvesh Patel, COPY FROM, COPY TO, CSV File, database, database research and development, dbrnd, export, import, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....