How to Export a PostgreSQL Table to CSV?

PostgreSQL is a powerful open-source relational database management system that provides various features including data export capabilities. One of the most common export formats is CSV (Comma-Separated Values), which allows easy data exchange between different applications and systems.

CSV is a simple file format where each row represents a record and fields are separated by commas. The first row typically contains column headers, making the data structure clear and readable.

Importance of Exporting PostgreSQL Tables to CSV

Exporting PostgreSQL tables to CSV format offers several advantages

  • Data Sharing Share data with team members who don't have database access

  • Application Integration Import data into Excel, Google Sheets, or other analysis tools

  • Data Backup Create portable backups outside the database system

  • Migration Transfer data between different database systems

Preparing for Export

Connect to PostgreSQL Database

Before exporting, establish a connection to your PostgreSQL database using either pgAdmin (GUI tool) or command line interface (CLI).

Using pgAdmin Open pgAdmin, right-click your server name in the Object Browser, select "Connect Server", and enter your credentials.

Using CLI Open terminal and connect using the psql command

psql -h [host] -p [port] -U [username] -d [database_name]

Identify Target Table

Navigate to your desired schema and locate the table you want to export. In pgAdmin, expand the server tree structure to find your table. In CLI, use \dt to list all tables in the current database.

Methods to Export Table to CSV

Method 1 Using pgAdmin

pgAdmin provides a user-friendly graphical interface for exporting data

  1. Right-click on the target table and select "Export" from the context menu

  2. In the Export dialog, choose "CSV" as the output format

  3. Specify the file path and name for your CSV file

  4. Configure options like delimiter character (default: comma), headers, quoting rules

  5. Click "OK" to start the export process

Method 2 Using COPY Command (CLI)

The COPY command provides more control and is ideal for automation

COPY table_name TO '/path/to/output.csv' DELIMITER ',' CSV HEADER;

For exporting specific columns

COPY (SELECT column1, column2 FROM table_name WHERE condition) 
TO '/path/to/output.csv' DELIMITER ',' CSV HEADER;

Method 3 Using \copy Meta-command

The \copy meta-command works from the client side and doesn't require superuser privileges

\copy table_name TO 'output.csv' DELIMITER ',' CSV HEADER;

Export Options and Customization

PostgreSQL provides several options to customize your CSV export

Option Description Example
DELIMITER Field separator character DELIMITER ';'
CSV Enable CSV format mode CSV
HEADER Include column names as first row CSV HEADER
QUOTE Character used for quoting CSV QUOTE '"'
ESCAPE Character used for escaping CSV ESCAPE ''

Example Complete Export Process

Here's a complete example exporting a sample employees table

-- Connect to database
\c company_db

-- Export entire table
COPY employees TO '/home/user/employees.csv' DELIMITER ',' CSV HEADER;

-- Export with custom query
COPY (SELECT emp_id, name, salary FROM employees WHERE salary > 50000) 
TO '/home/user/high_salary_employees.csv' DELIMITER ',' CSV HEADER;

The resulting CSV file will look like

emp_id,name,salary
101,John Doe,60000
102,Jane Smith,75000
103,Mike Johnson,55000

Common Issues and Solutions

  • Permission Denied Ensure you have write permissions to the target directory

  • File Path Issues Use absolute paths and ensure directory exists

  • Special Characters Use proper quoting and escape characters for data containing commas or quotes

  • Large Tables For very large tables, consider exporting in chunks using LIMIT and OFFSET

Conclusion

Exporting PostgreSQL tables to CSV format is straightforward using either pgAdmin's graphical interface or PostgreSQL's COPY command. The COPY command offers more flexibility and control, making it ideal for automated scripts and complex export requirements. Choose the method that best fits your workflow and technical requirements.

Updated on: 2026-03-17T09:01:38+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements