Skip to content

Instantly share code, notes, and snippets.

@ImLoaD
Last active August 4, 2021 19:46
Show Gist options
  • Save ImLoaD/0dd226107b505b0bef1b61b7f27405a7 to your computer and use it in GitHub Desktop.
Save ImLoaD/0dd226107b505b0bef1b61b7f27405a7 to your computer and use it in GitHub Desktop.
Export table from PostgreSQL
#!/bin/sh
# Prepare names
DEFAULT_DB_NAME="my_web_admin"
read -p "Enter database name [$DEFAULT_DB_NAME]: " DB_NAME
DB_NAME=${name:-$DEFAULT_DB_NAME}
read -p "Enter table name in db '$DB_NAME' name to make schema dump: " TABLE_NAME
DIR_NAME="$DB_NAME"
# Exit if table name not writen
if [ "$TABLE_NAME" = "" ]; then
echo "You loose (I need table name)"
exit
fi
mkdir $DIR_NAME
FILE_NAME="${DIR_NAME}/${TABLE_NAME}.sql"
echo "I will create a file called $FILE_NAME with data dump from ${TABLE_NAME}"
pg_dump -h localhost -U postgres -d $DB_NAME -t "\"$TABLE_NAME\"" > "$FILE_NAME"
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment