Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save beingsane/7e8df97728e15cb42c0810c62c93924e to your computer and use it in GitHub Desktop.

Select an option

Save beingsane/7e8df97728e15cb42c0810c62c93924e 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