Search code examples
postgresqlpg-restore

clone postgres database with new name


I have a users-production database on 157.157.35.333

I would like to clone it to another host as users-sandbox

here is what I tried:

    PRODUCTION_HOST=111.111.11.111
    SANDBOX_HOST=222.222.22.222

    echo "creating db on sanbox"
    psql -h ${SANDBOX_HOST} -U postgres  -c "CREATE DATABASE \"users-sandbox\";"

    pg_dump -h ${PRODUCTION_HOST} -U postgres  -d users-production -F c -b -v  | \
       pg_restore -C -c -h ${SANDBOX_HOST} -U postgres  -d users-sandbox -v

but this creates the database with the old name

how do I create with a new name?


Solution

  • https://www.postgresql.org/docs/current/static/app-pgrestore.html

    -d dbname

    Connect to database dbname and restore directly into the database.

    but!

    -C

    --create

    Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before connecting to it.

    When this option is used, the database named with -d is used only to issue the initial DROP DATABASE and CREATE DATABASE commands. All data is restored into the database name that appears in the archive.

    so remove -C from pg_restore arguments...

    (formatting of quotes mine)