PostgreSQL: Import/Export CSV files using psql

Updated: January 5, 2024 By: Guest Contributor Post a comment

Introduction

Working with databases often involves transferring data to and from external files for a variety of reasons such as data migration, backup, or analysis. PostgreSQL, a highly popular and powerful open-source relational database, provides utilities to facilitate this process with CSV files using the psql tool, which is the focus of this tutorial.

Prerequisites:

  • A working installation of PostgreSQL.
  • Basic knowledge of SQL commands and PostgreSQL syntax.
  • The psql command-line interface for interacting with PostgreSQL.

Exporting Data to a CSV File

To export data from a PostgreSQL table to a CSV file, you can use the COPY command with the TO keyword.

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

This command exports the ‘table_name’ content into a CSV file located at ‘/path/to/file.csv’. The ‘DELIMITER’ specifies the character that separates columns within the CSV, which is typically a comma. The ‘CSV HEADER’ option tells PostgreSQL to include column headings as the first row in the CSV file.

If you don’t have superuser access, or you want to invoke COPY from a client rather than the server’s file system, you can use the psql \\COPY command instead. This operates similarly but allows non-superusers to export data.

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

Handling Special Characters

When dealing with special characters like commas or newlines within your data, the CSV format will ensure these are appropriately quoted. However, if your delimiter happens to be the same as one of your data values, you may wish to define a custom delimiter:

COPY table_name TO '/path/to/file.csv' DELIMITER '|' CSV HEADER;

In this case, the pipe symbol is used instead of a comma.

Importing Data from a CSV File

To import data from a CSV file into a PostgreSQL table, use the COPY command with the FROM keyword:

COPY table_name FROM '/path/to/file.csv' DELIMITER ',' CSV HEADER;

This will read from the file and copy data into the ‘table_name’. It assumes the column structure in the CSV matches the destination table.

As with exporting, if you’re not using a superuser PostgreSQL account, you can use the client-side \\COPY command:

\\COPY table_name FROM '/path/to/file.csv' DELIMITER ',' CSV HEADER;

Advanced Options

Partial Import/Export

If you need to export or import only selected columns, you can specify them after the table name:

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

Conditional Import/Export

Sometimes, you may need to export or import data based on certain conditions. The COPY command allows subselects to accommodate this:

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

Troubleshooting

When working with import or export of CSV files, you may encounter several issues:

  • File access permissions: Ensure the PostgreSQL server user has the necessary read or write permissions on the CSV file and its directory.
  • Matching schemas: The structure (columns) of the CSV should match the structure of the destination table. Otherwise, consider specifying columns or adjusting the CSV file.
  • Data formatting: Pay attention to date and numeric formats, as they should conform to the default PostgreSQL format unless explicitly handled.

Conclusion

Using the COPY and \\COPY commands in PostgreSQL to import and export CSV files is a straightforward way to manage bulk data operations. While relatively easy to implement for basic uses, these commands also offer advanced options such as conditional data handling, custom delimiters, and column selection to accommodate complex scenarios. By understanding and applying these capabilities, database administrators and developers can efficiently facilitate data migration and backup processes.