postgres函数:从txt文件复制到数据库

时间:2017-06-24 13:36:02

标签: postgresql postgresql-9.3 postgresql-9.2 postgresql-9.4

我发现下面的函数将数据从csv复制到Postgres,它还从csv动态创建表,我想要类似的功能,但它应该用于文本文件。

我不是来自开发背景,我必须动态地将txt文件加载到Postgres表。

是否可以使用以下函数来处理txt文件?

create or replace function public.load_csv_file
(
target_table text,
csv_path text,
col_count integer
)

returns void as $$

declare

iter integer; -- dummy integer to iterate columns with
col text; -- variable to keep the column name at each iteration
col_first text; -- first column name, e.g., top left corner on a csv file or     spreadsheet

begin
set schema 'public';

create table insert_from_csv ();

-- add just enough number of columns
for iter in 1..col_count
loop
    execute format('alter table insert_from_csv add column col_%s text;', iter);
end loop;

-- copy the data from csv file
execute format('copy insert_from_csv from %L with delimiter '','' quote ''"'' csv ', csv_path);

iter := 1;
col_first := (select col_1 from insert_from_csv limit 1);

-- update the column names based on the first row which has the column names
for col in execute format('select unnest(string_to_array(trim(temp_table::text, ''()''), '','')) from temp_table where col_1 = %L', col_first)
loop
    execute format('alter table insert_from_csv rename column col_%s to %s', iter, col);
    iter := iter + 1;
end loop;

-- delete the columns row
execute format('delete from insert_from_csv where %s = %L', col_first, col_first);

-- change the temp table name to the name given as parameter, if not blank
if length(target_table) > 0 then
    execute format('alter table insert_from_csv rename to %I', target_table);
end if;

end;

$$ language plpgsql;

专家的任何帮助都会对我有所帮助。

2 个答案:

答案 0 :(得分:0)

Postgres对第一列的第一个字母有一些问题,如果它是大写,那么它给出错误并且与小写字母一起使用。

感谢erwin&米歇尔回应。

答案 1 :(得分:0)

我设法修改了这个函数,它将用' _'替换任何空格。并将列名重命名为小写。

create or replace function load_csv_file
(
target_table text,
csv_path text,
col_count integer
)

returns void 
SECURITY DEFINER
as $$

declare

iter integer; -- dummy integer to iterate columns with
col text; -- variable to keep the column name at each iteration
col_first text; -- first column name, e.g., top left corner on a csv file or     spreadsheet

begin
set schema 'test';

create table insert_from_csv ();

-- add just enough number of columns
for iter in 1..col_count
loop
execute format('alter table insert_from_csv add column col_%s text;', iter);
end loop;

-- copy the data from csv file
execute format('copy insert_from_csv from %L delimiter '','' csv ',     csv_path);

iter := 1;
col_first := (select col_1 from insert_from_csv limit 1);

-- update the column names based on the first row which has the column names
for col in execute format('select unnest(string_to_array(lower(replace(trim(insert_from_csv::text, ''()''),'' '',''_'')), '','')) from insert_from_csv where col_1 = %L', col_first)
loop
execute format('alter table insert_from_csv rename column col_%s to %s', iter, col);
iter := iter + 1;
end loop;

-- delete the columns row
execute format('delete from insert_from_csv where %I = %L', col_first, col_first);

-- change the temp table name to the name given as parameter, if not blank
if length(target_table) > 0 then
execute format('alter table insert_from_csv rename to %I', target_table);
end if;

end;

$$ language plpgsql;
相关问题