PSQL: Import & Export data

Posted on May 4, 2021
Tags: postgresql

Let’s say you have a table in production and would like to copy some data into your local postgres database so you can experiment a little bit. This can be easily achieved with the psql command \copy.

Open a psql connection to your prod database and for exporting the data run:

 \copy (SELECT * FROM trades) to '~/trades.csv' with csv;

This will create a CSV in your home directory named trades.csv.

Then open a psql connection to your local database and run:

\copy trades from '~/trades.csv' with csv;

And voilà, you have imported your production data into your local database.