Postgres COPY FROM csv file- No such file or directory

坚强是说给别人听的谎言 提交于 2019-12-02 22:27:59
Erwin Brandstetter

A couple of misconceptions:

1.

I'm in the /~ directory of my server

There is no directory /~. It's either / (root directory) or ~ (home directory of current user). It's also irrelevant to the problem.

2.

I set the search_path variable to geochat, the name of the database I'm working in

The search_path has nothing to do with the name of the database. It's for schemas inside the current database. You probably need to reset this.

3.
You are required to use the absolute path for your file. As documented in the manual here:

filename

The absolute path name of the input or output file.

4.
DELIMITER: just noise.

The default is a tab character in text format

5.
NULL: It's rather uncommon to use the actual string 'NULL' for a NULL value. Are you sure?

The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format.

My guess (after resetting search_path - or you schema-qualify the table name):

COPY geonames FROM '/path/to/file/US.txt';

Maybe a bit late, but hopefully useful:

Use \copy instead

https://wiki.postgresql.org/wiki/COPY

jvdw

Masten SG

The paths are relative to the PostgreSQL server, not the psql client.

Assuming you are running PostgreSQL 9.4, you can put US.txt in the directory /var/lib/postgresql/9.4/main/.

if you're running your COPY command from a script, you can have a step in the script that creates the COPY command with the correct absolute path.

MYPWD=$(pwd)
echo "COPY geonames FROM '$MYPWD/US.txt', DELIMITER E'\t';"
MYPWD=

you can then run this portion into a file and execute it

./step_to_create_COPY_with_abs_path.sh >COPY_abs_path.sql
psql -f COPY_abs_path.sql -d your_db_name
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!