How to do PostGres Data to CSV

Create a new empty database

createdb june-12-valendo

Import the data dump.sql into the new database:

psql june-12-valendo < dump.sql

Create the CSV file. It must first exist in order to be able to copy to it.

touch data-june-12.csv

Give PostGres Access to the Folder and Files

chmod a+rw ~/workspace/postgres_data_accessible_to_postgres

Access the database

psql june-12-valendo

List all tables and pick the interesting/important ones

\d

Copy using a SQL command

COPY interesting_field TO '/workspace/postgres_data_accessible_to_postgres/valendo_data-jun-12-2017.csv' DELIMITER ',' CSV HEADER;

Instead of using a SQL command, you can also use a database command. Note that, in that case, the relative value "~" is important for the file:

\copy ar_internal_metadata to '~/workspace/postgres_data_accessible_to_postgres/valendo_data-jun-12-2017.csv' csv header

https://stackoverflow.com/questions/1120109/export-postgres-table-to-csv-file-with-headings

| Back

Footerfull