Copy From csv to postgres - postgresql

I am having trouble using the copy from command in postgresql to import data in a csv file. I first created a table in postgresql with the following code.
create table sfmap (
Address varchar(100),
City varchar(50),
State varchar(5),
Zip varchar(6),
County varchar(20)
)
I then used the following code to copy the data in the table.
COPY sfmap(Address, City, State, County)
FROM 'H:\sfmap.csv'
WITH DELIMITER ','
CSV HEADER
Every time I do this, I get the follow error message.
ERROR: could not open file "H:\sfmap.csv" for reading: No such file or directory
SQL state: 58P01
I am sure the location of the file is correct. I have tried using a forward slash, two backwardslashes. I don't see what I am doing wrong.

COPY command must be run by a Postgres superuser. Might be worth checking that.

This is a permission issue. First, check if the file is 'read' accessible to the 'Others' group (assuming your work under Linux), and then check if the folder it is in is also 'read' accessible.

Related

How to deal with error:SQL Error [58P01]?

I'm learning PostgreSQL, and i run into problem:
I have created table and trying to export it, but I have all the time same error
SQL Error [58P01]: ERROR: could not open file "D:\Janis\Mācības\SQL\Test\typestest.txt" for writing: No such file or directory
Hint: COPY TO instructs the PostgreSQL server process to write a file. You may want a client-side facility such as psql's \copy.
Script that I'm running:
CREATE TABLE char_data_types (
varchar_column varchar(10),
char_column char(10),
text_column text
);
INSERT INTO char_data_types
VALUES
('abc', 'abc', 'abc'),
('defghi', 'defghi', 'defghi');
COPY char_data_types TO 'D:\Janis\Mācības\SQL\Test\typestest.txt'
WITH (FORMAT CSV, HEADER, DELIMITER '|');
I have taken off all possible security restriction from file typestest.txt
There is no directory D:\Janis\Mācības\SQL\Test on the PostgreSQL server. Note that COPY writes to the database server, no the client (the statement runs on the server, and the server cannot access the client machine).
To export the data to the client machine, you have to use psql so that you can use \copy:
\copy char_data_types TO 'D:\Janis\Mācības\SQL\Test\typestest.txt' WITH (FORMAT CSV, HEADER, DELIMITER '|')
The command has to be in a single line.

How to import a CSV file into PostgreSQL using Mac

I am trying to import data from a CSV file into PostgreSQL using pgAdmin. However, I am getting an error message when attempting to perform a COPY command.
ERROR: could not open file "/Users/brandoncruz/Desktop/Test File.csv" for reading: Permission denied
HINT: COPY FROM instructs the PostgreSQL server process to read a file. You may want a client-side facility such as psql's \copy.
SQL state: 42501
Below is the code I have attempted to use for the import.
CREATE TABLE roles(
role_id serial PRIMARY KEY,
role_name VARCHAR (255)
);
COPY roles FROM '/Users/brandoncruz/Desktop/Test File.csv' DELIMITER ',' CSV HEADER;
It looks about your permissions. You can change permissions of 'Test File.csv'. I mean Postgres user cannot read your file. After change permissions it must be copied successfully.
You might need to check following path if you are using pgAdmin:
pgAdmin > File > Preferences
Paths > Binary Paths
For postgreSQL binary path, you should find the location of PostgreSQL from your mac and save that:
i.e. C:\Program Files\PostgreSQL\12\bin (Windows)
For mac, check under Applications, Program Files, that depends and changes from Mac to Mac.

Export to CSV file with windows path [duplicate]

I use PostgreSQL 9.4.1
My query:
copy(select * from city) to 'C:\\temp\\city.csv'
copy(select * from city) to E'C:\\temp\\city.csv'
ERROR: relative path not allowed for COPY to file
********** Error **********
ERROR: relative path not allowed for COPY to file SQL state: 42602
As with this case, it seems likely that you are attempting to use copy from a computer other than the one which hosts your database. copy does I/O from the database host machine's local file system only. If you have access to that filesystem, you can adjust your attempt accordingly. Otherwise, you can use the \copy command in psql.
I am using pgAdmin v1.5 . The first query is
select table_name from information_schema.tables where table_catalog = 'ofbiz' order by table_name
Then I press button download, pgAdmin will return a csv file, is result set of first query.
It could be late but i think it can be helpful.
On Windows, make sure the output directory has gain the read/write right for Everyone (or you can specific user name).
Using slash(/) instead of backslash(), example
COPY DT1111 TO 'D:/TEST/DT1111_POST.CSV' DELIMITER ',' CSV HEADER;
TLDR: Make sure you also have write permissions in your copy-to location!
I had the exact same first error, ERROR: relative path not allowed for COPY to file, even though I used '/tmp/db.csv' (which is not a relative path).
In my case, the error message was quite misleading, since I was on the host machine, had an absolute filepath and the location existed. My problem was that I used the bitnami postgres:12 docker image, and the tmp folder in the container belongs to root there, while postgres and psql use the postgres user. My solution was to create an export folder there and transform the ownership to the postgres user:
mkdir /tmp/export
chown postgres:postgres /tmp/export
Then I was able to use COPY tablename TO '/tmp/export/db.csv'; successfully.

postgresql csv file import

When I try to load a csv file into postgresql. I use the following command.
CREATE TABLE population(
country char(80),
year integer,
population integer
);
COPY population FROM '/Users/chittampalliyashaswini/Desktop/population.csv'
DELIMITER ',' CSV HEADER;
Then I get the following error.
ERROR: could not open file "/Users/chittampalliyashaswini/Desktop/population.csv" for reading: Permission denied
The message is clear, the user postgresql is running under has no permission to read the file.
You can move it to /tmp and/or fix the permissions on the path. I typically switch to the postgres user and run a less to check.
If it is a one time thing, I suggest you simply move it to /tmp/ and use that path.

How to export the resulting data in PostgreSQL to .CSV?

I use PostgreSQL 9.4.1
My query:
copy(select * from city) to 'C:\\temp\\city.csv'
copy(select * from city) to E'C:\\temp\\city.csv'
ERROR: relative path not allowed for COPY to file
********** Error **********
ERROR: relative path not allowed for COPY to file SQL state: 42602
As with this case, it seems likely that you are attempting to use copy from a computer other than the one which hosts your database. copy does I/O from the database host machine's local file system only. If you have access to that filesystem, you can adjust your attempt accordingly. Otherwise, you can use the \copy command in psql.
I am using pgAdmin v1.5 . The first query is
select table_name from information_schema.tables where table_catalog = 'ofbiz' order by table_name
Then I press button download, pgAdmin will return a csv file, is result set of first query.
It could be late but i think it can be helpful.
On Windows, make sure the output directory has gain the read/write right for Everyone (or you can specific user name).
Using slash(/) instead of backslash(), example
COPY DT1111 TO 'D:/TEST/DT1111_POST.CSV' DELIMITER ',' CSV HEADER;
TLDR: Make sure you also have write permissions in your copy-to location!
I had the exact same first error, ERROR: relative path not allowed for COPY to file, even though I used '/tmp/db.csv' (which is not a relative path).
In my case, the error message was quite misleading, since I was on the host machine, had an absolute filepath and the location existed. My problem was that I used the bitnami postgres:12 docker image, and the tmp folder in the container belongs to root there, while postgres and psql use the postgres user. My solution was to create an export folder there and transform the ownership to the postgres user:
mkdir /tmp/export
chown postgres:postgres /tmp/export
Then I was able to use COPY tablename TO '/tmp/export/db.csv'; successfully.