PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Advanced
    • Indexes
    • Views
    • Triggers
    • Administration
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
    • Window Functions
Home / PostgreSQL Tutorial / Export PostgreSQL Table To CSV File

Export PostgreSQL Table To CSV File

Summary: in this tutorial, you will learn various techniques to export data from PostgreSQL tables to CSV files.

In the previous tutorial, we showed you how to import data from a CSV file into a table. We will use the persons table that we created for importing data from a CSV file.

posgresql export csv

Let’s check data of the persons table.

1
2
3
4
SELECT
   *
FROM
   persons;

posgresql import csv from pgAdmin tool persons table

We have two records in the table.

Export data from a table to CSV using COPY statement

The easiest way to export data of a table to a CSV file is to use COPY statement. For example, if you want to export the data of the persons table to a CSV file named persons_db.csv in the C:\tmp folder, you can use the following statement:

1
COPY persons TO 'C:\tmp\persons_db.csv' DELIMITER ',' CSV HEADER;

PostgreSQL exports all data from all columns of the persons table to the persons_db.csv file.

postgresql export csv

In some cases, you want to export data from just some columns of a table to a CSV file. To do this, you specify the column names together with table name after COPY keyword. For example, the following statement exports data from the first_name, last_name, and email  columns of the persons table to person_partial_db.csv

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

postgresql export csv partially

If you don’t want to export the header, which contains the column names of the table, just remove the HEADER flag in the COPY statement. The following statement exports only data from the email column of the persons table to a CSV file.

1
2
COPY persons(email)
TO 'C:\tmp\persons_email_db.csv' DELIMITER ',' CSV;

postgresql export csv partially without header

Notice that the CSV file name that you specify in the COPY command must be written directly by the server. It means that the CSV file must reside on the database server machine, not your local machine. The CSV file also needs to be writable by the user that PostgreSQL server runs as.

Export data from a table to CSV file using the \copy command

In case you have the access to a remote PostgreSQL database server, but you don’t have sufficient privileges to write to a file on it, you can use the PostgreSQL built-in command \copy.

The \copy command basically runs the COPY statement above. However, instead of server writing the CSV file, psql writes the CSV file, transfers data from the server to your local file system. To use \copy command, you just need to have sufficient privileges to your local machine. It does not require PostgreSQL superuser privileges.

For example, if you want to export all data of the persons table into persons_client.csv file, you can execute the \copy command from the psql client as follows:

1
\copy (SELECT * FROM persons) to 'C:\tmp\persons_client.csv' with csv

In this tutorial, we have shown you how to use COPY statement and \copy command to export data from a table to CSV files.

  • Was this tutorial helpful ?
  • YesNo
Previous Tutorial: Import CSV File Into PostgreSQL Table
Next Tutorial: PostgreSQL CREATE DATABASE

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

  • SELECT
  • ORDER BY
  • SELECT DISTINCT
  • WHERE
  • LIMIT
  • FETCH
  • IN
  • BETWEEN
  • LIKE
  • IS NULL
  • ALIAS
  • Joins
  • INNER JOIN
  • LEFT JOIN
  • SELF-JOIN
  • FULL OUTER JOIN
  • Cross Join
  • Natural Join
  • GROUP BY
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • GROUPING SETS
  • CUBE
  • ROLLUP
  • Subquery
  • ANY
  • ALL
  • EXISTS
  • INSERT
  • UPDATE
  • UPDATE Join
  • Upsert
  • DELETE

Managing Databases

  • Create Database
  • Alter Database
  • Rename Database
  • Drop Database
  • Copy a Database
  • Get Database Object Sizes

Managing Tables

  • PostgreSQL Data Types
  • Create Table
  • Select Into
  • Create Table As
  • SERIAL
  • Sequences
  • Identity Column
  • Alter Table
  • Rename Table
  • Add Column
  • Drop Column
  • Change Column’s Data Type
  • Rename Column
  • Drop Table
  • Temporary Table
  • Truncate Table

Database Constraints

  • Primary Key
  • Foreign Key
  • CHECK Constraint
  • UNIQUE Constraint
  • NOT NULL Constraint

PostgreSQL Data Types

  • Boolean
  • CHAR, VARCHAR, and TEXT
  • NUMERIC
  • Integer
  • SERIAL
  • DATE
  • TIMESTAMP
  • Interval
  • TIME
  • UUID
  • JSON
  • HSTORE
  • Array
  • User-defined Data Types

Managing Database Schemas

  • Schema
  • Create Schema
  • Alter Schema
  • Drop Schema

Conditional Expressions & Operators

  • CASE
  • COALESCE
  • NULLIF
  • CAST

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 DROP TRIGGER
  • PostgreSQL NTILE Function
  • PostgreSQL PERCENT_RANK Function
  • PostgreSQL CUME_DIST Function
  • PostgreSQL Sequences
  • PostgreSQL LAG Function
  • PostgreSQL LEAD Function
  • PostgreSQL NTH_VALUE Function
  • PostgreSQL LAST_VALUE Function
  • PostgreSQL FIRST_VALUE Function

Site Info

  • Home
  • PostgreSQL Cheat Sheet
  • Resources
  • About Us
  • Contact Us
  • Privacy Policy

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

⤒