且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从命令行导入 PostgreSQL CSV

更新时间:2023-01-15 19:24:24

如 The PostgreSQL Documentation (II. PostgreSQL 客户端应用程序 - psql) 您可以使用开关 -cpsql(PostgreSQL 交互式终端)/代码>.您的选择是:

As stated in The PostgreSQL Documentation (II. PostgreSQL Client Applications - psql) you can pass a command to psql (PostgreSQL interactive terminal) with the switch -c. Your options are:

执行 SQL COPY 命令但文件在客户端读取,内容路由到服务器.

perform the SQL COPY command but the file is read on the client and the content routed to the server.

psql -c "copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv"

(最初在这个答案中提到的客户端选项)

(client-side option originally mentioned in this answer)

读取服务器上的文件(当前用户需要有必要的权限):

reads the file on the server (current user needs to have the necessary permissions):

psql -c "COPY tbname FROM '/tmp/the_file.csv' delimiter '|' csv;"

读取服务器上文件所需的数据库角色:

the DB roles needed for reading the file on the server:

COPY 命名文件或命令只允许数据库超级用户或被授予默认角色之一的用户pg_read_server_filespg_write_server_filespg_execute_server_program

COPY naming a file or command is only allowed to database superusers or users who are granted one of the default roles pg_read_server_files, pg_write_server_files, or pg_execute_server_program

PostgreSQL 服务器进程也需要访问该文件.

also the PostgreSQL server process needs to have access to the file.