PostgreSQL pg_dump 创建sql脚本,但它不是sql脚本: is there a way to get pg_dump to create a standard sql script?

标签 postgresql pg-dump

我正在运行 pg_dump 来创建一个脚本来自动创建如下系统:

pg_dump --dbname=postgresql://postgres:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09f98948399b0c1c2c7dec0dec0dec1" rel="noreferrer noopener nofollow">[email protected]</a>:5432/OHDSI -t webapi.* > webapi.sql

这会创建一个 sql 脚本,但它并不是真正的 sql 脚本,因为它包含如下所示的代码。

当此脚本作为 SQL 脚本运行时,它会失败并给出如下所示的错误。

有没有办法让pg_dump创建一个标准sql的sql脚本,并且可以作为sql脚本执行?

pg_dump 生成的 sql 代码示例:

COPY webapi.cohort_version (asset_id, comment, description, version, asset_json, archived, created_by_id, created_date) FROM stdin;
\.


--
-- Data for Name: concept_of_interest; Type: TABLE DATA; Schema: webapi; Owner: ohdsi_admin_user
--

COPY webapi.concept_of_interest (id, concept_id, concept_of_interest_id) FROM stdin;
1   4329847 4185932
2   4329847 77670
3   192671  4247120
4   192671  201340

运行 pg_dump 生成的脚本时出现错误:

--
-- Name: penelope_laertes_uni_pivot id; Type: DEFAULT; Schema: webapi; Owner: ohdsi_admin_user
--
ALTER TABLE ONLY webapi.penelope_laertes_uni_pivot ALTER COLUMN id SET DEFAULT nextval('webapi.penelope_laertes_uni_pivot_id_seq'::regclass)

--
-- Data for Name: achilles_cache; Type: TABLE DATA; Schema: webapi; Owner: ohdsi_admin_user
--
COPY webapi.achilles_cache (id, source_id, cache_name, cache) FROM stdin

Error executing: COPY webapi.achilles_cache (id, source_id, cache_name, cache) FROM stdin
.  Cause: org.postgresql.util.PSQLException: ERROR: COPY from stdin failed: COPY commands are only supported using the CopyManager API.
  Where: COPY achilles_cache, line 1
Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.jdbc.RuntimeSqlException: Error executing: COPY webapi.achilles_cache (id, source_id, cache_name, cache) FROM stdin
.  Cause: org.postgresql.util.PSQLException: ERROR: COPY from stdin failed: COPY commands are only supported using the CopyManager API.
  Where: COPY achilles_cache, line 1
    at org.yaorma.database.Database.executeSqlScript(Database.java:344)
    at org.yaorma.database.Database.executeSqlScript(Database.java:332)
    at org.nachc.tools.fhirtoomop.tools.build.postgres.build.A04_CreateAtlasWebApiTables.exec(A04_CreateAtlasWebApiTables.java:29)
    at org.nachc.tools.fhirtoomop.tools.build.postgres.build.A04_CreateAtlasWebApiTables.main(A04_CreateAtlasWebApiTables.java:19)
Caused by: org.apache.ibatis.jdbc.RuntimeSqlException: Error executing: COPY webapi.achilles_cache (id, source_id, cache_name, cache) FROM stdin
.  Cause: org.postgresql.util.PSQLException: ERROR: COPY from stdin failed: COPY commands are only supported using the CopyManager API.
  Where: COPY achilles_cache, line 1
    at org.apache.ibatis.jdbc.ScriptRunner.executeLineByLine(ScriptRunner.java:109)
    at org.apache.ibatis.jdbc.ScriptRunner.runScript(ScriptRunner.java:71)
    at org.yaorma.database.Database.executeSqlScript(Database.java:342)
    ... 3 more
Caused by: org.postgresql.util.PSQLException: ERROR: COPY from stdin failed: COPY commands are only supported using the CopyManager API.
  Where: COPY achilles_cache, line 1
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2365)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:355)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:490)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:408)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:329)
    at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:315)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:291)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:286)
    at org.apache.ibatis.jdbc.ScriptRunner.executeStatement(ScriptRunner.java:190)
    at org.apache.ibatis.jdbc.ScriptRunner.handleLine(ScriptRunner.java:165)
    at org.apache.ibatis.jdbc.ScriptRunner.executeLineByLine(ScriptRunner.java:102)
    ... 5 more

--- 编辑------------------------------------------------

接受的答案中的 --inserts 方法正是给了我我所需要的。

我最终这样做了:

pg_dump --inserts --dbname=postgresql://postgres: [email protected] :5432/OHDSI -t webapi.* > webapi.sql

最佳答案

您用来恢复转储的客户端工具无法处理来自(非标准)COPY 的数据。命令被混合到脚本中。您需要psql恢复这样的转储。

您可以使用--inserts pg_dump的选项创建包含 INSERT 的转储陈述而不是 COPY 。恢复速度会更慢,但可以与更多客户端工具配合使用。

但是,您想要获得标准 SQL 脚本的愿望是无望的。 PostgreSQL 在很多方面扩展了标准,因此无法使用标准 SQL 脚本转储数据库。请注意,例如,索引不是由 SQL 标准定义的。如果您希望将 PostgreSQL 转储传输到不同的 RDBMS,您将会失望。那就更难了。

关于PostgreSQL pg_dump 创建sql脚本,但它不是sql脚本: is there a way to get pg_dump to create a standard sql script?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75044656/

相关文章:

postgresql - 转储数据库时跳过违反唯一约束

postgresql - Postgres 恢复丢失的权限

postgresql - 如何在不重新启动的情况下重新加载 Postgres.app 配置?

sql - 当一行在另一个表中匹配时排除组

ruby-on-rails - 使用 Rails 将几何字段添加到 PostGIS

postgresql - Spring Boot 与 Postgres jsonb 的集成

SQL 过程错误

postgresql - 在 PostgreSQL 上创建数据库转储时出错

postgresql - docker 中的 pg_dump 服务器和 pg_dump 版本不匹配

postgresql - PostgreSQL COPY 方法需要什么锁?