postgresql - pg_restore 输入文件似乎是文本格式转储。请使用 psql

标签 postgresql ansible psql

我有一个 Ansible 任务,它将在 postgresql 数据库中加载一个数据库转储文件。

name: Apply prod dump file
  become_user: postgres
  shell: "PGPASSWORD={{ db_password }} psql -h localhost -d {{ db_name }} -U {{ db_user }} -f {{ server_path }}/dump.sql"
  when: run_db_restore is defined and run_db_restore
  tags: django.restore

问题是转储文件包含 CREATE ROLE 和 CREATE DATABASE 部分,我不想这样做,因为它会覆盖我用 Ansible 创建的进程,所以我想预制

pg_restore --no-owner --data-only

在一个sql文件上,所以我只能创建表和导入数据。问题是我该如何解决

psql -h localhost -d {{ db_name }} -U {{ db_user }} -f {{ server_path }}/dump.sql

这样我就可以在数据库中正确导入数据。

这个函数 pg_restore --no-owner --data-only -U admin -d database dump.sql 给我看

pg_restore:[archiver] 输入文件似乎是文本格式转储。请使用 psql。

最佳答案

除非您先编辑 SQL 文件,否则您无法执行此操作,这是一项具有挑战性且容易出错的自动任务。

我建议您更改程序并改为使用自定义格式转储。然后您可以使用 pg_restore 恢复它们,并且只恢复您需要的部分。

既然你说你在转储中有用户,看来你是用 pg_dumpall 创建的。如果您不想让用户在那里,请不要这样做。

关于postgresql - pg_restore 输入文件似乎是文本格式转储。请使用 psql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931541/

相关文章:

mysql - Cassandra 还是 MySQL/PostgreSQL?

Ansible - 如何清空文件夹

docker - 在 Docker 中运行时,Fetch 模块返回 "unable to calculate the checksum of the remote file",但不在 Docker 中时工作正常

python - 减少将 POStgreSQL 表引入具有 500000 行的 Pandas 的执行时间的替代方法?

postgresql - 使用psql时如何在postgres中选择模式?

ruby-on-rails - 处理 PostgreSQL 创建表、关联和 Rails

python - 如何在 sqlalchemy column_property 中拥有原始 sql?

postgresql - Greenplum,是否可以将 CSV 导出到远程服务器?

linux - 在通过 Ansible 执行命令之前切换到 root(使用 "sudo su -")

postgresql - 如何能够垂直滚动终端以查看大表中的所有数据