【问题标题】:Postgres SQL - error: must be superuser to copy to or from a filePostgresql - 错误:必须是超级用户才能复制到文件或从文件复制
【发布时间】:2017-11-26 18:19:44
【问题描述】:

我已经复制了函数(从其中一个门户网站并进行了相应修改)以将数据从 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 文件复制数据。 没有超级用户可以复制数据吗?

【问题讨论】:

标签: postgresql csv privileges postgresql-copy


【解决方案1】:

看起来 Insert_postgres.csv 位于通常没有读/写权限的 C 驱动器中。将文件移动到您的目录中,其中读取/写入至少授予某些组或每个人。 希望能解决问题

【讨论】:

  • 我尝试将文件移动到 c:\Test\Insert_postgres.csv,但仍然遇到同样的错误。注意:我使用的 postgres 用户不是超级用户。
  • 使用户成为超级用户:ALTER USER username WITH SUPERUSER;使用户不再是超级用户:ALTER USER username WITH NOSUPERUSER;只允许用户创建数据库: ALTER USER username CREATEDB;您还可以使用 CREATEROLE 和 CREATEUSER 来授予用户权限而不使他们成为超级用户。
  • 嗨 Priya,感谢您的回答,但我拥有的用户不是超级用户,因此无法更改其权限。我想要实现的是,我将自动化这个过程,所以我想通过点击一次又一次地重复这个活动,所以我正在寻找解决方案来复制而不是超级用户。
  • 在对这个主题进行了广泛的研究之后......你不能在没有成为超级用户的情况下使用 COPY 语句。故事结局。 :-)
【解决方案2】:

尝试在终端中运行以下命令。这允许我们使用 COPY 命令,因为您不能使用 COPY 命令而不是超级用户。

ALTER USER <username> WITH SUPERUSER;

【讨论】:

    猜你喜欢
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    相关资源
    最近更新 更多