Skip to content

Instantly share code, notes, and snippets.

@braynm
Last active October 18, 2021 07:04
Show Gist options
  • Save braynm/f3b65417bcdbf6190424e5f422cb628b to your computer and use it in GitHub Desktop.
Save braynm/f3b65417bcdbf6190424e5f422cb628b to your computer and use it in GitHub Desktop.
copy from|to csv in postgresql
-- Save without header
copy ((select "name", "email from "user") to '/absolute-path-to-directory/filename.csv' delimiter ',' csv;
-- Save with header
copy ((select "name", "email from "user") to '/absolute-path-to-directory/filename.csv' delimiter ',' csv header;
-- When having issue `must be superuser or a member of pg_write_server_files role COPY to a file`, add a `\` in front of `copy`
\copy ((select "name", "email from "user") to '/absolute-path-to-directory/filename.csv' delimiter ',' csv;
-- copy csv to table
\copy "user" ("organizationId") from '<enter path>' delimiter ',' csv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment