Skip to content

Instantly share code, notes, and snippets.

@davidahopp
Forked from sharmaeklavya2/psql_to_csv.sh
Last active August 28, 2023 19:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save davidahopp/b688aa6310dd9b1f42839108f36c968b to your computer and use it in GitHub Desktop.
Save davidahopp/b688aa6310dd9b1f42839108f36c968b to your computer and use it in GitHub Desktop.
Export all tables including headers in a postgres database to a set of CSV files
#!/bin/bash
DB_NAME="$USER"
DBMS_SHELL="psql"
DBMS_USER="postgres"
#if [ "$1" = '--help' ]; then
if [[ ( "$1" == '--help' ) || ( "$1" == '-h' ) ]]; then
echo "usage: $0 [DB_NAME] [DBMS_SHELL]"
echo "default DB_NAME is your username"
echo "default DBMS_SHELL is 'psql'"
echo "default DBMS_USER is 'postgres'"
exit 0
fi
if [ -n "$1" ]
then DB_NAME="$1"
fi
if [ -n "$2" ]
then DBMS_SHELL="$2"
fi
if [ -n "$3" ]
then DBMS_USER="$3"
fi
alias echo='>&2 echo'
mkdir -p "$DB_NAME"
echo "Fetching table list ..."
$DBMS_SHELL "$DB_NAME" -U $DBMS_USER -c "copy (select table_name from information_schema.tables where table_schema='public') to STDOUT;" > "$DB_NAME/tables.txt"
dbms_success=$?
if ! [ $dbms_success ]
then exit 4
fi
echo "Fetching tables ..."
readarray tables < "$DB_NAME/tables.txt"
for t in ${tables[*]}; do
$DBMS_SHELL -d "$DB_NAME" -U $DBMS_USER -c "copy (select * from $t) to STDOUT with delimiter ',' CSV HEADER;" > "$DB_NAME/$t.csv"
done
@zetos
Copy link

zetos commented Apr 2, 2018

This was very useful !! Thank you.

@kolyan007
Copy link

Thanks. I'm really interesting to use this script but during execution, I got below. How to make it work?
Fetching table list ...
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_NZ:en",
LC_ALL = (unset),
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Fetching tables ...

@NguyenSon2310
Copy link

I have error like this
image
How can I fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment