Created
September 15, 2018 11:08
-
-
Save nicnilov/d7fc6785e9e89958dbd4457b2e4d385f to your computer and use it in GitHub Desktop.
Dump postgresql database to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DB_SCHEMA="public" | |
DB_USER="postgres" | |
DB_HOST="localhost" | |
DB_PORT="db_port" | |
DB_NAME="db_name" | |
# DB_PASS="" to be passed from shell | |
psql "postgresql://$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/$DB_NAME" -Atc "select tablename from pg_tables where schemaname='$DB_SCHEMA'" $DB_NAME |\ | |
while read TBL; do | |
psql "postgresql://$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/$DB_NAME" -c "\copy $DB_SCHEMA.$TBL to stdout with (format csv, header true, delimiter ',');" $DB_NAME > $TBL.csv | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment