Skip to content

Instantly share code, notes, and snippets.

@ferhatelmas
Forked from nepsilon/postgres-import-export-csv.md
Created October 28, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferhatelmas/02a03c0dc555465d6dc4 to your computer and use it in GitHub Desktop.
Save ferhatelmas/02a03c0dc555465d6dc4 to your computer and use it in GitHub Desktop.
Importing and Exporting CSV files with PostgreSQL
-- Import CSV into table t_words
COPY t_words FROM '/path/to/file.csv' DELIMITER ',' CSV;
-- You can tell quote char with QUOTE
-- and change delimiter with DELIMITER
-- Import CSV into table t_words,
-- telling what columns to use
COPY t_words("name", "count", "def") FROM 'file.csv' DELIMITER ',' CSV;
-- Export table to a CSV file
COPY t_words TO 'file.csv' DELIMITER ',' CSV HEADER;
-- Export custom results to a CSV file
COPY (SELECT word, def FROM t_words) TO 'file.csv' CSV;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment