我正在运行pg_ump来创建一个脚本,以自动创建如下所示的系统:

pg_dump --dbname=postgresql://postgres:ohdsi@127.0.0.1:5432/OHDSI -t webapi.* > webapi.sql

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

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

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

Code sample from sql generated by pg_dump:

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

Error seen when running the script generated by 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

--- EDIT ------------------------------------

被接受的答案中的--Inserts方法正好满足了我的需求.

我最终做了这件事:

Pg_Dump--插入--dbname=postgresql://postgres:ohdsi@127.0.0.1:5432/OHDSI-t Webapi.*>Webapi.sql

推荐答案

用于恢复转储的客户端工具无法处理(非标准)COPY命令混合到脚本中的数据.你需要psql才能恢复这样的垃圾场.

您可以使用pg_dump--inserts选项来创建包含INSERT条语句而不是COPY条语句的转储.恢复速度会较慢,但可以使用更多的客户端工具.

然而,您希望获得标准的SQL脚本是没有希望的.PostgreSQL在许多方面扩展了该标准,因此不能使用标准SQL脚本转储数据库.请注意,例如,索引不是由SQL标准定义的.如果您希望将PostgreSQL转储传输到不同的RDBMS,您将会失望.这就更难了.

Postgresql相关问答推荐

使用regexp获取表名

用于JSON数组的带有组合条件的Postgres JSONB Select 查询

如何使用docker/docker-compose转到本地主机?

Pogresql性能中的枚举与文本数据类型

复合索引列的顺序导致不同的计划

Postgres查询按月和年分组的总计数

使用postgres在go测试容器中使用tern迁移

使用Helm设置PostgreSQL配置

将数组的所有元素循环到jsonb中并修改值

我应该 Select 哪种数据类型?

postgres 的密码

在 Ubuntu 11.04 服务器中启用对 postgresql 的 PHP 支持

是否可以在 Postgres 中存储一个 1 字节的数字?

如何确定 NULL 是否包含在 Postgres 的数组中?

使用 pg-promise 插入多条记录

如何在 PostgreSQL 中插入多行

PostgreSQL 中跨多个表的索引

PostgreSQL 9 在 Windows 上安装:Unable to write inside TEMP environment path.

用于将布尔列排序为 true、null、false 的 SQL

PL/pgSQL 中的 BREAK 语句