Skip to content

Instantly share code, notes, and snippets.

@maykbrito
Last active October 14, 2020 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maykbrito/8c1d39d1a6f4a6bf8d285320cf926252 to your computer and use it in GitHub Desktop.
Save maykbrito/8c1d39d1a6f4a6bf8d285320cf926252 to your computer and use it in GitHub Desktop.

How to dump and import PostgreSQL database

Below some step by step with bash functions example on how to dump your database and restore it to your db server.

✅ Dump the source database to a file.

function pgDumpDB() {
    echo 'Type db user: ' && read DBUSER;
    echo 'Type db name: ' && read DBNAME;
    pg_dump -U $DBNAME -O $DBNAME $DBNAME.sql  
}

✅ Copy the dump file to the remote server.

✅ Create a new database in the remote server: CREATE DATABASE name

✅ Restore the dump file on the remote server:

function pgImportDB() {
    echo 'Type db user: ' && read DBUSER
    echo 'Type db pass: ' && read DBPASS
    echo 'Type db name: ' && read DBNAME
    psql -U $DBUSER -c "CREATE DATABASE $DBNAME"
    psql -U $DBUSER -d $DBNAME -f $DBNAME.sql
}

Source

https://www.postgresqltutorial.com/postgresql-copy-database/

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