Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何将101个CSV文件一次导入PostgreSQL数据库?_Database_Postgresql_Csv_File Upload_Psql - Fatal编程技术网

Database 如何将101个CSV文件一次导入PostgreSQL数据库?

Database 如何将101个CSV文件一次导入PostgreSQL数据库?,database,postgresql,csv,file-upload,psql,Database,Postgresql,Csv,File Upload,Psql,我在MacOS终端上设置了psql。它连接到我的PostgreSQL数据库,通过Amazon RDS运行 我有100个CSV文件(名称为1,2,3,4,最多100个)。我想批量进口。我看到有一些脚本(),但我不知道如何运行脚本 我试着复制和粘贴这个脚本- for x in $(ls <folder_name>*.csv); do psql -c "copy table_name from '$x' csv" also; done 以美元表示的x的(ls*.csv); 也要执行ps

我在MacOS终端上设置了psql。它连接到我的PostgreSQL数据库,通过Amazon RDS运行

我有100个CSV文件(名称为1,2,3,4,最多100个)。我想批量进口。我看到有一些脚本(),但我不知道如何运行脚本

我试着复制和粘贴这个脚本-

for x in $(ls <folder_name>*.csv); 
do psql -c "copy table_name from '$x' csv" also; done
以美元表示的x的
(ls*.csv);
也要执行psql-c“从“$x”csv复制表_名称”;完成
我收到了这些错误-

db=> for x in $(ls <folder_name>*.csv); 
ERROR:  syntax error at or near "for"
LINE 1: for x in $(ls <folder_name>...
        ^
db=> do psql -c "copy <table_name> from '$x' csv" also; done
ERROR:  syntax error at or near "psql"
LINE 1: do psql -c "copy <table_name> from '$x' csv" also;
db=>用于x,单位为美元(ls*.csv);
错误:“for”处或附近出现语法错误
第1行:对于以美元表示的x(ls。。。
^
db=>do psql-c“从“$x”csv复制”;完成
错误:“psql”处或附近出现语法错误
第1行:也执行psql-c“从“$x”csv复制”;
你能帮我a)找出批量导入这些文件的正确脚本,b)找出如何执行脚本吗


注意-所有文件都将转到已存在的同一个表中。

考虑此表:

CREATE TABLE t (id INT,description TEXT);
和以下文件

file1.csv 文件2.csv 执行以下shell脚本:

#!/bin/bash
path="/home/user/files/"

for f in $path*.csv;do

    cat $f | psql testdb -c "COPY yourtable FROM STDIN DELIMITER ',' CSV HEADER"

done
这就是您的数据:

$ psql testdb -c "SELECT * FROM t"
 id | description 
----+-------------
  1 | foo
  2 | bar
(2 Zeilen)

如果要将所有文件加载到同一个表中,并且可以排除标题,则可以选择将它们连接到单个csv中

bash-$ head -1 /folder_name/file1.csv > combined_file.csv

bash-$ cat folder_name/*.csv | grep -v 'headerpattern' >>combined_file.csv

postgres#从'combined_file.csv'DELIMITER'复制您的表。

这是一个shell脚本,不是plpgsql脚本。你试过从shell运行吗?我在终端窗口运行。我的印象是终端窗口是外壳。这是准确的吗?我已经了解到shell是在访问PSQL之前的终端窗口。我在空白的终端窗口(AFAIK’shell’中给出了一个尝试,并接收到这个错误——PSQL:不能连接到服务器:没有这样的文件或目录是本地运行的服务器,并且接受UNIX域套接字”/tMP/.S.PGSQL(5432)上的连接。?您似乎无法通过
psql
连接到服务器。数据库和
psql
在同一台机器上吗?只需说明一下:您必须将此代码保存到文本文件中,并在终端中执行,例如使用
/myfile.sh
。如果您的文件不可执行,请授予它使用
chmod+x myfile.sh执行的权限,这是我的错误。我已经用psql login命令替换了“yourserver”来访问我的数据库。较新版本出现此错误-/Users/One/Documents/import_script2.txt:第6行:查找匹配时出现意外EOF`“'/Users/One/Documents/import_script2.txt:第10行:语法错误:意外的文件结尾您可以在问题中添加这些CSV文件的示例吗?只是为了确保问题不是文件结构本身。如果每个csv文件都有自己的头,这种方法可能会“失败”吗?
$ psql testdb -c "SELECT * FROM t"
 id | description 
----+-------------
  1 | foo
  2 | bar
(2 Zeilen)
bash-$ head -1 /folder_name/file1.csv > combined_file.csv

bash-$ cat folder_name/*.csv | grep -v 'headerpattern' >>combined_file.csv