从命令行导入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客户端应用程序 - psql ),您可以通过 psql
c $ c> -c :
psql -cCOPY tbname FROM'/ tmp / the_file .csv'delimiter'|'csv;
I've been using the psql Postgres terminal to import CSV files into tables using the following
COPY tbname FROM
'/tmp/the_file.csv'
delimiter '|' csv;
which works fine except that I have to be logged into the psql terminal to run it.
I would like to know if anyone knows of a way to do a command similar to this from the Linux shell command line similar to how Postgres allows a shell command like bellow
/opt/postgresql/bin/pg_dump dbname > /tmp/dbname.sql
This allows the dumping of a database from the Linux shell without being logged into psql terminal.
As stated in The PostgreSQL Documentation (II. PostgreSQL Client Applications - psql) you can pass a command to psql
with the switch -c
:
psql -c "COPY tbname FROM '/tmp/the_file.csv' delimiter '|' csv;"
这篇关于从命令行导入PostgreSQL CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!