How to import and export a CSV file to and from Postgres

You can simply import and export a comma seperated values file (CSV) by using the command line or the pgadmin SQL interface. There are slightly different commands between both approaches.

Import

By using the pgadmin SQL interface you need to add a public infront of the table name:

 copy public."TableName" from '/data/my_csv_file.csv' using delimiters ';'
 

By using the command line you do not need the public appendix:

copy TableName from '/data/my_csv_file.csv' using delimiters ';'

Export

Pretty the same difference applies to the export commands.

Pgadmin SQL interface:

\copy public."TableName" to '/data/my_csv_file.csv' delimiters ';'

Command line:

\copy TableName to '/data/my_csv_file.csv' delimiters ';'

One thought on “How to import and export a CSV file to and from Postgres

Leave a comment