从本地文件复制到远程数据库

COPY to remote databse from a local file

有类似的问题,但没有解决我的问题: 我在我的远程数据库中定义了一个存储过程。作为它操作的一部分,SP 必须从本地文件批量插入(通过我在本地计算机上的应用程序 运行ning): 程序:

CREATE OR REPLACE FUNCTION upsert_realtime(path text, traveltime_table regclass)
  RETURNS void AS
$BODY$
BEGIN   
    EXECUTE format('COPY temp_table(link_id,start_time,end_time,travel_time,interval_time, travel_mode) FROM %L DELIMITER '';'' CSV', path);
    --...     
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION upsert_realtime(text, regclass)
  OWNER TO postgres;

提供给 SP 的路径在本地机器上:

select * from upsert_realtime('/home/.../travel_time.txt','realtime_travel_time');

因此我得到这个错误:

terminate called after throwing an instance of 'soci::postgresql_soci_error'
  what():  Cannot execute query. ERROR:  could not open file "/home/.../travel_time.txt" for reading: No such file or directory

我唯一想到的是以某种方式自动将文件 scp 发送到 postgresql 服务器,然后 运行 SP,这不是最好的主意。 你能帮我找到解决办法吗?谢谢 谢谢

你是using the wrong path。这是错误的常见来源。都是关于 "from the viewpoint of the server."

COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible by the PostgreSQL user (the user ID the server runs as) and the name must be specified from the viewpoint of the server. When PROGRAM is specified, the server executes the given command and reads from the standard output of the program, or writes to the standard input of the program. The command must be specified from the viewpoint of the server, and be executable by the PostgreSQL user. When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server.

如果您没有可用的路径,则需要

  • 将文件移动到服务器,并使用正确的路径,
  • 在服务器上调用一个可以访问您本地文件的程序,
  • 使用psql连接到远程服务器并读取本地文件。