pg_restore 使用-C 选项不创建数据库
pg_restore with -C option does not create the database
我正在使用 pg_dump 和 pg_restore 来备份和恢复 postgres 数据库。
这是文档中与此问题相关的一些信息
对于Pg_restore,-C选项说明如下
-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 一起使用,我也会收到以下错误
pg_restore: [archiver (db)] connection to database "test" failed:
FATAL: > database "test" does not exist
根据描述,-C 选项应该已经创建了丢失的数据库。但是在我的情况下不是这样。
以下是我备份和恢复的步骤:
- 使用pg_dump备份数据库
pg_dump -N backup -d test --format custom -v -h xxhostxx -p 5432 -U xxuserxx --lock-wait-timeout 300000 -f test_pg_dump.dmp
注意:不使用 -C 选项,因为它仅对纯文本格式有意义
删除了测试数据库
使用pg_resore恢复数据库
pg_restore -C -d test -v -h xxhostxx -p 5432 -U xxuserxx test_pg_dump.dmp**
我不明白这是什么问题!我做错了什么吗?
如果需要更多信息,请告诉我。
以下引用并不代表您可能认为的意思。我也不得不读了三遍才明白他们在说什么。
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最初会连接到-d指定的数据库。它不会创建该数据库。它使用您正在恢复的存档中的名称创建一个数据库,并将数据恢复到该数据库中。
正如@Eelke 所说 - 你在文件中写了 'create database' 所以这个数据库在你 运行ning 脚本时不存在......这就是为什么总是 'postgres'数据库。试试这个:
pg_restore -C -d postgres -v -h xxhostxx -p 5432 -U xxuserxx test_pg_dump.dmp**
这应该:
- 连接到 postgres 数据库
- 创建测试数据库
- 断开与 postgres 的连接并连接到测试
- 上传数据到数据库
当然要检查谁是 postgres 数据库的所有者 - 在大多数情况下,您必须 运行 作为用户 'postgres'。
我从来没有用过,所以这个命令创建了数据库
createdb -h HOST -U USER -W DB_NAME
然后执行pg restore
pg_restore -d DB_NAME -v -h HOST -p PORT -U USER DUMP_FILE.dump**
故事结束
来自man pg_restore
EXAMPLES
部分
Assume we have dumped a database called mydb into a custom-format dump file:
$ pg_dump -Fc mydb > db.dump
To drop the database and recreate it from the dump:
$ dropdb mydb
$ pg_restore -C -d postgres db.dump
The database named in the -d switch can be any database existing in the cluster; pg_restore only uses it to issue the CREATE DATABASE command for mydb.
With -C, data is always restored into the database name that appears in the dump file.
To reload the dump into a new database called newdb:
$ createdb -T template0 newdb
$ pg_restore -d newdb db.dump
Notice we don't use -C, and instead connect directly to the database to be restored into. Also note that we clone the new database from template0 not
template1, to ensure it is initially empty.
因此在你的情况下:
$ createdb -h xxhostxx -p 5432 -U xxuserxx -T template0 test
$ pg_restore -h xxhostxx -p 5432 -U xxuserxx -d test db.dump
我正在使用 pg_dump 和 pg_restore 来备份和恢复 postgres 数据库。
这是文档中与此问题相关的一些信息 对于Pg_restore,-C选项说明如下
-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 一起使用,我也会收到以下错误
pg_restore: [archiver (db)] connection to database "test" failed: FATAL: > database "test" does not exist
根据描述,-C 选项应该已经创建了丢失的数据库。但是在我的情况下不是这样。
以下是我备份和恢复的步骤:
- 使用pg_dump备份数据库
pg_dump -N backup -d test --format custom -v -h xxhostxx -p 5432 -U xxuserxx --lock-wait-timeout 300000 -f test_pg_dump.dmp
注意:不使用 -C 选项,因为它仅对纯文本格式有意义
删除了测试数据库
使用pg_resore恢复数据库
pg_restore -C -d test -v -h xxhostxx -p 5432 -U xxuserxx test_pg_dump.dmp**
我不明白这是什么问题!我做错了什么吗? 如果需要更多信息,请告诉我。
以下引用并不代表您可能认为的意思。我也不得不读了三遍才明白他们在说什么。
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最初会连接到-d指定的数据库。它不会创建该数据库。它使用您正在恢复的存档中的名称创建一个数据库,并将数据恢复到该数据库中。
正如@Eelke 所说 - 你在文件中写了 'create database' 所以这个数据库在你 运行ning 脚本时不存在......这就是为什么总是 'postgres'数据库。试试这个:
pg_restore -C -d postgres -v -h xxhostxx -p 5432 -U xxuserxx test_pg_dump.dmp**
这应该:
- 连接到 postgres 数据库
- 创建测试数据库
- 断开与 postgres 的连接并连接到测试
- 上传数据到数据库
当然要检查谁是 postgres 数据库的所有者 - 在大多数情况下,您必须 运行 作为用户 'postgres'。
我从来没有用过,所以这个命令创建了数据库
createdb -h HOST -U USER -W DB_NAME
然后执行pg restore
pg_restore -d DB_NAME -v -h HOST -p PORT -U USER DUMP_FILE.dump**
故事结束
来自man pg_restore
EXAMPLES
部分
Assume we have dumped a database called mydb into a custom-format dump file:
$ pg_dump -Fc mydb > db.dump
To drop the database and recreate it from the dump:
$ dropdb mydb
$ pg_restore -C -d postgres db.dump
The database named in the -d switch can be any database existing in the cluster; pg_restore only uses it to issue the CREATE DATABASE command for mydb.
With -C, data is always restored into the database name that appears in the dump file.
To reload the dump into a new database called newdb:
$ createdb -T template0 newdb
$ pg_restore -d newdb db.dump
Notice we don't use -C, and instead connect directly to the database to be restored into. Also note that we clone the new database from template0 not
template1, to ensure it is initially empty.
因此在你的情况下:
$ createdb -h xxhostxx -p 5432 -U xxuserxx -T template0 test
$ pg_restore -h xxhostxx -p 5432 -U xxuserxx -d test db.dump