Skip to content

Instantly share code, notes, and snippets.

@afraz-khan
Last active April 29, 2022 10:41
Show Gist options
  • Save afraz-khan/dd25f8d55457cb60f50b097a105fc0a0 to your computer and use it in GitHub Desktop.
Save afraz-khan/dd25f8d55457cb60f50b097a105fc0a0 to your computer and use it in GitHub Desktop.
Export Postgresql Data to CSV File
  1. Open a terminal and login to your database using below command (LINUX)
$ psql -h [HOST-STRING] -d [DATABASE-NAME] -U [USERNAME]
  1. Now use below command to export data from your database to CSV file
$ \copy (Table/Query) to [path-to-csvfile] csv header

Examples:

  • \copy "user" to /home/myuser/Desktop/thesite-stg-user-data.csv csv header
  • \copy (select id,email,phone from "user" order by id) to /home/myuser/Desktop/thesite-stg-user-data.csv csv header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment