我用

pg_dump db_production > postgres_db.dump

然后使用scp将其复制到localhost。

现在当我导入我的本地db时,它给出了一个错误

pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

使用命令行

pg_restore -d db_development postgres_db.dump

当前回答

我也一直在为这个问题挣扎。这是dump和restore命令的组合,对我来说很有效:

pg_dump -Ft -C -h database_host -U username database > DATA.dump

恢复

pg_restore -x --no-owner -d database DATA.dump

如果您想在您的DB中保持相同的访问权限(acl),请删除-x标志。为此,在数据库中必须具有相同的角色和用户。

https://www.postgresql.org/docs/15/app-pgdump.html

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

其他回答

为了使用pg_dump创建与pg_restore兼容的备份,在创建转储时必须使用——format=custom / -Fc。

从文档中可以看出:

输出适合输入到pg_restore的自定义格式的存档。

所以你的pg_dump命令可能是这样的:

pg_dump --file /tmp/db.dump --format=custom --host localhost --dbname my-source-database --username my-username --password

你的pg_restore命令:

pg_restore --verbose --clean --no-acl --no-owner --host localhost --dbname my-destination-database /tmp/db.dump

psql -U <username> -d <database-name> -h <host-name> -f <backup.sql> . p

我也一直在为这个问题挣扎。这是dump和restore命令的组合,对我来说很有效:

pg_dump -Ft -C -h database_host -U username database > DATA.dump

恢复

pg_restore -x --no-owner -d database DATA.dump

如果您想在您的DB中保持相同的访问权限(acl),请删除-x标志。为此,在数据库中必须具有相同的角色和用户。

https://www.postgresql.org/docs/15/app-pgdump.html

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

如果你有一个完整的数据库转储:

PGPASSWORD="your_pass" psql -h "your_host" -U "your_user" -d "your_database" -f backup.sql

但是,如果您的模式是分开保存的,那么这就行不通了。然后需要禁用数据插入触发器,类似于pg_restore——disable-triggers。然后你可以使用这个:

cat database_data_only.gzip | gunzip | PGPASSWORD="your_pass" psql -h "your_host" -U root "your_database" -c 'SET session_replication_role = replica;' -f /dev/stdin

另一方面,我认为这是postgres的一个非常不幸的缺点。在pg_dump中创建转储的默认方式与pg_restore不兼容。然而,加上一些额外的关键字,它就是。WTF ?

如果您恢复。sql文件。 在pgAdmin中创建一个新数据库。 进入终端,浏览.sql文件所在的文件夹/目录。然后在终端中写入以下命令。

让: 超自然用户后

Examaple:

sudo -u postgres psql newDb < restoreDb.sql