Postgres SQL - 错误: 必须是超级用户才能从文件复制或复制到文件

28 浏览
0 Comments

Postgres SQL - 错误: 必须是超级用户才能从文件复制或复制到文件

我从一个网站中复制了一个函数(并进行了相应修改)以将数据从csv文件复制到表中。

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;

并将参数传递为

select load_csv_file(\'Customer\',\'C:\\Insert_postgres.csv\' ,4)

但是却收到错误信息

错误: 必须是超级用户才能从文件复制或复制到文件

提示: 任何人都可以将数据复制到标准输出或从标准输入中复制数据。 psql的\\ copy命令也适用于任何人。

我的想法是,我将创建一个自动化测试,如果我想要在不同的实例上进行测试,则测试应该自动创建函数并从csv文件中复制数据。

有没有绕过超级用户进行数据复制的方法?

admin 更改状态以发布 2023年5月20日
0
0 Comments

在终端中运行以下命令。这使我们可以使用COPY命令,因为您不能在不成为超级用户的情况下使用COPY命令。

ALTER USER  WITH SUPERUSER;

0
0 Comments

看起来Insert_postgres.csv在C盘中,通常没有读写权限。 把文件移到一个您有读写权限的目录,至少要授予给一些组或每个人。 希望它能解决问题。

0