Skip to content

Instantly share code, notes, and snippets.

@jukkatupamaki
Last active April 27, 2021 13:04
Show Gist options
  • Save jukkatupamaki/21cea73fb252d1e9330e3fa5bf14a700 to your computer and use it in GitHub Desktop.
Save jukkatupamaki/21cea73fb252d1e9330e3fa5bf14a700 to your computer and use it in GitHub Desktop.
How to import/export data from/to a CSV file in psql CLI (PostgreSQL)

Open psql and connect to your database.

Export a query to a CSV file

Use the \copy command to export data from a remote or local database to a local CSV file.

\copy (select id, name from products) to 'my-local-file.csv' csv header

Import records from a CSV file to a table

Use the \copy command to import records from a local CSV file to a remote or local database.

\copy products from 'my-local-file.csv' csv header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment