在Postgresql中复制:解释为相对路径的绝对路径

时间:2021-11-08 21:45:31

I am running this statement in a Django app:

我在Django应用程序中运行此语句:

c = connections['default'].cursor()    query="copy (select * from analysis.\"{0}\") to STDOUT DELIMITER ',' CSV HEADER;".format(view_name)            with open(csvFile,'w') as f:                c.copy_expert(query,f)            f.close()

It does not create the correct csv file. Some of the values appear to be in the wrong columns. I am trying to test the SQL statement by running it in POSTGRESQL:

它不会创建正确的csv文件。某些值似乎位于错误的列中。我试图通过在POSTGRESQL中运行它来测试SQL语句:

 copy (select * from analysis."S03_2005_activity_140807_153431_with_geom") to 'C:/djangoProjects/web_output/csvfiles/S03_2005_activity_140807_153431_with_geom.csv' DELIMITER ',' CSV HEADER;

It gives me: "ERROR: relative path not allowed for COPY to file". I have looked into the issue and it appears to typically be one of two issues: 1. confusing '\' and '/'. My slashes should be correct. 2. The server being on a different computer. I thought this may be my issue as the database is located on an external computer, but I have the connection in my Postgresql. It also runs from Django so I'm not sure why it isn't working from PG Admin.

它给了我:“错误:COPY不允许相对路径存档”。我调查了这个问题,它似乎通常是两个问题之一:1。混淆'\'和'/'。我的斜杠应该是正确的。 2.服务器位于不同的计算机上。我认为这可能是我的问题,因为数据库位于外部计算机上,但我在Postgresql中有连接。它也是从Django运行所以我不确定为什么它不能从PG Admin工作。

1 个解决方案

#1


If you want to store data / get data from your local machine and communicate with a Postgres server on a different, remote machine, you cannot simply use COPY.

如果要从本地计算机存储数据/获取数据并与另一台远程计算机上的Postgres服务器通信,则不能简单地使用COPY。

Try the meta-command \copy in psql. It's a wrapper for the SQL COPY command and uses local files.

在psql中尝试meta-command \ copy。它是SQL COPY命令的包装器并使用本地文件。

Your filename should work as is on a Windows machine, but Postgres interprets it as a local filename on the server, which is probably a Unix derivate. And there the filename would have to start with '/'.

您的文件名应该在Windows机器上工作,但Postgres将其解释为服务器上的本地文件名,这可能是Unix衍生物。文件名必须以'/'开头。

#1


If you want to store data / get data from your local machine and communicate with a Postgres server on a different, remote machine, you cannot simply use COPY.

如果要从本地计算机存储数据/获取数据并与另一台远程计算机上的Postgres服务器通信,则不能简单地使用COPY。

Try the meta-command \copy in psql. It's a wrapper for the SQL COPY command and uses local files.

在psql中尝试meta-command \ copy。它是SQL COPY命令的包装器并使用本地文件。

Your filename should work as is on a Windows machine, but Postgres interprets it as a local filename on the server, which is probably a Unix derivate. And there the filename would have to start with '/'.

您的文件名应该在Windows机器上工作,但Postgres将其解释为服务器上的本地文件名,这可能是Unix衍生物。文件名必须以'/'开头。