Export Data to CSV Format in PostgreSQL


The objective of this article is to enable the system administrator to convert postgreSQL data in CSV format

1. Connect PostgreSQL

Use psql command to connect to the PostgreSQL database.

psql -p 5433 -U dba database-name

NOTE: 5433 is my PostgreSQL port number.

2. Make DB Server Export Ready Now

Type “\o /data/tabledata.csv“, it tell PostgreSQL that next query result will be exported to file “/data/tabledata.csv”.

dbname=> \o /data/tabledata.csv

3. Query to Export

Now, issue a normal query.

dbname=> select * from tablename;

dbname=> \q

The entire query’s result will be exported to /data/tabledata.csv

Leave a Reply

Your email address will not be published. Required fields are marked *