在 PostgreSQL 中,如何使用 COPY 命令插入数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32271378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In PostgreSQL, how to insert data with COPY command?
提问by Erwin Brandstetter
I have problem when run 1 project NodeJs with PostgreSQL database.
I have error when trying to insert data in pgAdmin using the COPY
command.
使用 PostgreSQL 数据库运行 1 个项目 NodeJs 时遇到问题。尝试使用该COPY
命令在 pgAdmin 中插入数据时出错。
COPY beer (name, tags, alcohol, brewery, id, brewery_id, image) FROM stdin;
Bons Voeux blonde 9.5 Brasserie Dupont 250 130 generic.png
This data in gist:
要点中的这些数据 :
This error:
这个错误:
ERROR: syntax error at or near "Bons"
SQL state: 42601
Character: 1967
回答by Erwin Brandstetter
COPY tbl FROM STDIN;
is not supported by pgAdmin.
You get a plain syntax error because Postgres gets the data as SQL code.
pgAdmin 不支持。
你会得到一个简单的语法错误,因为 Postgres 将数据作为 SQL 代码获取。
Fourpossible solutions:
四种可能的解决方案:
1.Use a multi-row INSERT
instead:
1.改用多行INSERT
:
INSERT INTO beer(name, tags, alcohol, brewery, id, brewery_id, image)
VALUES
('Bons Voeux', 'blonde', 9.5, 'Brasserie Dupont', 250, 130, 'generic.png')
, ('Boerke Blond', 'blonde', 6.8, 'Brouwerij Angerik', 233, 287 'generic.png')
;
Note the different (SQL) syntax for values as string or numeric literals.
请注意作为字符串或数字文字的值的不同 (SQL) 语法。
You can generate the data with pg_dump
using --inserts
. Related:
您可以生成数据pg_dump
使用--inserts
。有关的:
2.Or call your script on the command line using psql
. As system user postgres
:
2.或者在命令行上使用psql
. 作为系统用户postgres
:
psql -f beer.sql -U my_login_role -d db_name
Database (-d
) and login role (-U
for "User") can be omitted if defaults are ok. Syntax examples:
如果默认值没问题,可以省略数据库 ( -d
) 和登录角色(-U
对于“用户”)。语法示例:
Be sure there is an end-of-data marker (\.
) for default text
format. (You have that.) The manual:
确保\.
默认text
格式有数据结束标记 ( ) 。(你有那个。)手册:
End of data can be represented by a single line containing just backslash-period (
\.
). An end-of-data marker is not necessary when reading from a file, since the end of file serves perfectly well; it is needed only when copying data to or from client applications using pre-3.0 client protocol.
数据的结尾可以由仅包含反斜杠句点 (
\.
)的单行表示。从文件读取时不需要数据结束标记,因为文件结束非常好;仅在使用 3.0 之前的客户端协议将数据复制到客户端应用程序或从客户端应用程序复制数据时才需要它。
3.Or move your data to a separate file on the server, say 'beer_data.csv' and use COPY .. FROM 'filename'
in your script:
3.或者将您的数据移动到服务器上的一个单独文件中,比如“beer_data.csv”并COPY .. FROM 'filename'
在您的脚本中使用:
COPY beer (name, tags, alcohol, brewery, id, brewery_id, image)
FROM '/path/to/beer_data.csv';
Which works either way. You need superuser privileges, though. The manual:
无论哪种方式都有效。但是,您需要超级用户权限。手册:
[...]
COPY
naming a file or command is only allowed to database superusers or users who are granted one of the default rolespg_read_server_files
,pg_write_server_files
, orpg_execute_server_program
, since it allows reading or writing any file or running a program that the server has privileges to access.
[...]
COPY
命名文件或命令只允许数据库超级用户或被授予默认角色之一的用户pg_read_server_files
,pg_write_server_files
, 或pg_execute_server_program
,因为它允许读取或写入任何文件或运行服务器有权访问的程序。
(pg_read_server_files
, pg_write_server_files
and pg_execute_server_program
are new in Postgres 11.)
( pg_read_server_files
,pg_write_server_files
并且pg_execute_server_program
是 Postgres 11 中的新内容。)
4.Or read a file local to the clientwith the psql meta-command \copy
. See:
回答by Claudio
First step:
第一步:
create Database belgianbeers on pgAdmin.
在 pgAdmin 上创建数据库 belgianbeers。
Second step: Open prompt and running this command line:
第二步:打开提示符并运行此命令行:
psql -U postgres -d belgianbeers -a -f beers.sql
psql -U postgres -d belgianbeers -a -f beers.sql
This command line running e update database tables.
此命令行运行 e 更新数据库表。
-U = username postgres
-U = 用户名 postgres