postgresql csv file import - postgresql

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.

Related

ERROR COPY import data from a csv file into a table in my Postgres DB

I am trying to import data from a csv file into a table in my DB, but keep getting an error.
SQL QUERY
BEGIN;
Copy csv_geometry_tamplate(facility_gis_number,facility_geometry)
from '/home/developer/tmp/facility_Polygon_testing.csv'
DELIMITER ','
CSV HEADER;
COMMIT;
{code}
ERROR: could not open file "/home/developer/tmp/facility_Polygon_testing.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.
It seems that the user access DB for location /home/developer/tmp/ did not have access permission.
I moved the file to /tmp/facility_Polygon_testing.csv, then changed permissions with chmod 777. This seem to have solved the issue.

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 from postgresql

I want to export a postgresql table to a csv file.
I have tried two ways, however both are unsuccessful for different reasons.
In the first case, you can see what I run and what I get bellow:
COPY demand.das_april18_pathprocess TO '/home/katerina/das_april18_pathprocess.csv' DELIMITER ',' CSV HEADER;
No such file or directory
SQL state: 58P01
I need to mention that in the location /home/katerina/ I have created an empty file named das_april18_pathprocess.csv, for which I modified the Permission settings to allow Read and Write.
In my second try, the query is executed without any errors but I cannot see the csv file. The command that I run is the following:
COPY demand.das_april18_pathprocess TO '/tmp/das_april18_pathprocess.csv' DELIMITER ',' CSV HEADER;
In the /tmp directory there is no cvs file.
Any advice on how to export the table to csv file with any way is really appreciated!
Ah, you run into a common problem -- you're creating a file on the server's filesystem, not your local filesystem. That can be a pain.
You can, however, COPY TO STDOUT, then redirect the result.
If you're using linux or another unix, the easiest way to do this is from the command line:
$ psql <connection options> -c "COPY demand.das_april18_pathprocess TO STDOUT (FORMAT CSV)" > das_april18_pathprocess.csv
copy ( select * from demand.das_april18_pathprocess) to '/home/katerina/das_april18_pathprocess.csv' with CSV header ;

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.

Copy From csv to postgres

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.