从命令行导入 PostgreSQL CSV
PostgreSQL CSV import from command line
我一直在使用 psql Postgres 终端将 CSV 文件导入到表格中,方法如下
COPY tbname FROM
'/tmp/the_file.csv'
delimiter '|' csv;
除了我必须登录到 psql 终端才能 运行 它工作正常。
我想知道是否有人知道从 Linux shell 命令行执行类似命令的方法,类似于 Postgres 允许 shell 命令的方式吼叫
/opt/postgresql/bin/pg_dump dbname > /tmp/dbname.sql
这允许从 Linux shell 转储数据库,而无需登录到 psql 终端。
如 PostgreSQL 文档 (II. PostgreSQL Client Applications - psql) 中所述,您可以将命令传递给 psql
(PostgreSQL 交互式终端)开关 -c
。您的选择是:
1,客户端 CSV:\copy
元命令
执行 SQL COPY
命令,但在客户端读取文件并将内容路由到服务器。
psql -c "\copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv"
(中最初提到的客户端选项)
2。服务器端 CSV:SQLCOPY
命令
读取服务器上的文件(当前用户需要有必要的权限):
psql -c "COPY tbname FROM '/tmp/the_file.csv' delimiter '|' csv;"
读取服务器上的文件所需的数据库角色:
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 服务器进程也需要访问该文件。
最灵活的方法是使用 shell HERE document
,它允许您在查询中使用 shell 变量,甚至在(双引号或单引号)内:
#!/bin/sh
THE_USER=moi
THE_DB=stuff
THE_TABLE=personnel
PSQL=/opt/postgresql/bin/psql
THE_DIR=/tmp
THE_FILE=the_file.csv
${PSQL} -U ${THE_USER} ${THE_DB} <<OMG
COPY ${THE_TABLE} FROM '${THE_DIR}/${THE_FILE}' delimiter '|' csv;
OMG
为了完成前面的,我建议:
psql -d your_dbname --user=db_username -c "COPY tbname FROM '/tmp/the_file.csv' delimiter '|' csv;"
已接受答案中的解决方案仅适用于服务器,当执行查询的用户将有权读取文件时,如 this SO answer 中所述。
否则,更灵活的方法是将 SQL 的 COPY
命令替换为 psql
's "meta-command" called \copy
which which takes all the same options as the "real" COPY, but is run inside the client(最后不需要 ;
):
psql -c "\copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv"
As per docs,\copy
命令:
Performs a frontend (client) copy. This is an operation that runs an SQL COPY command, but instead of the server reading or writing the specified file, psql reads or writes the file and routes the data between the server and the local file system. This means that file accessibility and privileges are those of the local user, not the server, and no SQL superuser privileges are required.
另外,如果the_file.csv
在第一行包含表头,可以通过在上述命令末尾添加header
来识别:
psql -c "\copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv header"
我一直在使用 psql Postgres 终端将 CSV 文件导入到表格中,方法如下
COPY tbname FROM
'/tmp/the_file.csv'
delimiter '|' csv;
除了我必须登录到 psql 终端才能 运行 它工作正常。
我想知道是否有人知道从 Linux shell 命令行执行类似命令的方法,类似于 Postgres 允许 shell 命令的方式吼叫
/opt/postgresql/bin/pg_dump dbname > /tmp/dbname.sql
这允许从 Linux shell 转储数据库,而无需登录到 psql 终端。
如 PostgreSQL 文档 (II. PostgreSQL Client Applications - psql) 中所述,您可以将命令传递给 psql
(PostgreSQL 交互式终端)开关 -c
。您的选择是:
1,客户端 CSV:\copy
元命令
执行 SQL COPY
命令,但在客户端读取文件并将内容路由到服务器。
psql -c "\copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv"
(
2。服务器端 CSV:SQLCOPY
命令
读取服务器上的文件(当前用户需要有必要的权限):
psql -c "COPY tbname FROM '/tmp/the_file.csv' delimiter '|' csv;"
读取服务器上的文件所需的数据库角色:
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
PostgreSQL 服务器进程也需要访问该文件。
最灵活的方法是使用 shell HERE document
,它允许您在查询中使用 shell 变量,甚至在(双引号或单引号)内:
#!/bin/sh
THE_USER=moi
THE_DB=stuff
THE_TABLE=personnel
PSQL=/opt/postgresql/bin/psql
THE_DIR=/tmp
THE_FILE=the_file.csv
${PSQL} -U ${THE_USER} ${THE_DB} <<OMG
COPY ${THE_TABLE} FROM '${THE_DIR}/${THE_FILE}' delimiter '|' csv;
OMG
为了完成前面的
psql -d your_dbname --user=db_username -c "COPY tbname FROM '/tmp/the_file.csv' delimiter '|' csv;"
已接受答案中的解决方案仅适用于服务器,当执行查询的用户将有权读取文件时,如 this SO answer 中所述。
否则,更灵活的方法是将 SQL 的 COPY
命令替换为 psql
's "meta-command" called \copy
which which takes all the same options as the "real" COPY, but is run inside the client(最后不需要 ;
):
psql -c "\copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv"
As per docs,\copy
命令:
Performs a frontend (client) copy. This is an operation that runs an SQL COPY command, but instead of the server reading or writing the specified file, psql reads or writes the file and routes the data between the server and the local file system. This means that file accessibility and privileges are those of the local user, not the server, and no SQL superuser privileges are required.
另外,如果the_file.csv
在第一行包含表头,可以通过在上述命令末尾添加header
来识别:
psql -c "\copy tbname FROM '/tmp/the_file.csv' delimiter '|' csv header"