The Wayback Machine - https://web.archive.org/web/20240118171521/https://www.geeksforgeeks.org/postgresql-export-postgresql-table-to-csv-file/
Open In App
Related Articles

PostgreSQL – Export PostgreSQL Table to CSV file

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article we will discuss the process of exporting a PostgreSQL Table to a CSV file. Here we will see how to export on the server and also on the client machine.

For Server-Side Export:

Use the below syntax to copy a PostgreSQL table from the server itself:

Syntax: COPY Table_Name TO 'Path/filename.csv' CSV HEADER;

Note: If you have permission to perform a read/write operation on the server-side then use this command.

Example:

First, let’s create a table with columns id, first_name, last_name, and email to the database:

CREATE TABLE students(
   id SERIAL PRIMARY KEY,
   first_name VARCHAR,
   last_name VARCHAR,
   email VARCHAR UNIQUE
);

Let’s insert some data into our students table:

INSERT INTO students(first_name, last_name, email)
VALUES('Virender', 'Sehwag', 'virender.sehwag@gfg.com'),
    ('Hardik', 'Pandiya', 'hardik.Pandiya@gfg.com'),
    ('Shreyas', 'Iyer', 'shreyas.iyer@gfg.com'),
    ('Rishabh', 'Pant', 'rishabh.pant@gfg.com');

Now check the data in the table:

SELECT * FROM students;

Output:

Now export the above table as a CSV file.

COPY students TO '/tmp/student1.csv' CSV HEADER;

Note: Make sure that the path that you specify should have read/write permission.

If everything works fine then it should look like this:

The CSV file would look like below:

We can also specify columns that we want to export or write a query for the data.

COPY (SELECT first_name FROM students) TO '/tmp/student.csv' CSV HEADER;

Output:

The CSV file would look like below:

CSV File

For Client-Side Export:

Use the below syntax for client-side export of CSV file:

Syntax: \copy Table_Name to 'Path/filename.csv' CSV HEADER

If you do not have permission to perform a read/write operation on the server-side and want to copy the table to the client-side machine then use this command.

Let’s use the students table here also.

Execute the below command to export the table to a CSV file.

\copy students to '/tmp/students.csv' CSV HEADER

Output:

The CSV file would look like below:

You can give a query to select data here also.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 23 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials