使用新名称克隆 postgres 数据库

clone postgres database with new name

我在 157.157.35.333

上有一个 users-production 数据库

我想将它克隆到另一台主机上 users-sandbox

这是我尝试过的:

    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

但这会创建旧名称的数据库

如何使用新名称创建?

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

-d dbname

Connect to database dbname and restore directly into the database.

但是!

-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.

所以从 pg_restore 个参数中删除 -C...

(我的引号格式)