How to import csv data into postgres table - postgresql

I tried to import csv file data into postgres table. Running the following line as pgscript in pgAdmin
\copy users_page_rank FROM E'C:\\Users\\GamulinN\\Desktop\\users page rank.csv' DELIMITER ';' CSV
it returned an error:
[ERROR ] 1.0: syntax error, unexpected character
Does anyone know what could be wrong here? I checked this post but couldn't figure out what's the problem.

To import file into postgres with COPY you need one of the following:
1) Connect with psql to the DB and run your comand:
\copy users_page_rank FROM E'C:\\Users\\GamulinN\\Desktop\\users page rank.csv' DELIMITER ';' CSV
It will copy the file from current computer to the table. Details here.
2) Connect with any tool to the DB and run this SQL script:
COPY users_page_rank FROM E'C:\\Users\\GamulinN\\Desktop\\users page rank.csv' DELIMITER ';' CSV
It will copy the file from the server with postgres to the table. Details here. (With this command you can only COPY from files in postgresql data directory. So you will need to transfer files there first.)

Related

Import multiple csv.gz format into postgres table on windows

I am having multiple of csv.gz files in local computer which I want to upload into postgres table without unzipping the file. I had refer to this questions (Loading zipped CSV(filename.csv.gz) file into PostgreSQL table) and used this query
\COPY TABLE FROM PROGRAM 'gzip -dc my_file.csv.gz' DELIMITER ',' CSV HEADER NULL;
when I run it on sql shell it shown this notification "gzip is not recognized as an internal or external command". Could anyone suggest me how to do it correctly

PostgresSQL unable to read csv files on my desktop

I am trying to import a CSV file into postgresSQL, however, I keep getting the error that no such file exists or directory.
this is the line of code I execute copy mu_data from
copy mu_data from 'users/mysurname/Desktop/FILE.CSV' DELIMITER ',' CSV
HEADER;
Can anyone suggest how to fix this?
copy is a command run on the server side. So unless your Postgres server happens to be on your localhost, the file very likely doesn't exist from the view of the server.
So one solution is you to transfer the file to the servers filesystem somehow. Or, if you're using the psql command line tool (or at least can use it for this task), you can use the \copy command there.

PostgreSQL: export data to csv file

I want to export the data in a table of some PostgreSQL database to a csv file.
Since the standard copy command does not work, I tried the following:
\copy (SELECT * FROM persons) to 'C:\tmp\persons_client.csv' with csv
just as in
http://www.postgresqltutorial.com/export-postgresql-table-to-csv-file/ .
The path seems to be correct; however, I get the error message
FEHLER: Syntaxfehler bei »\«
LINE 1: \copy [...]
which means that there is a syntax error at the "\" sign before the copy statement.
Any ideas what I missed?
By the way, this is not the real problem I'm currently facing.
Actually I was trying to import a csv file but unfortunately I do not seem to have sufficient privileges and when just using "COPY", the permission will be denied. so I tried to import the file using "\copy" but still get the same error message I get when trying to export using "\copy".
\copy (SELECT * FROM persons) to 'C:\tmp\persons_client.csv' with csv would not work in pgAdmin, because \copy is an pslq metacommand:
Performs a frontend (client) copy. This is an operation that runs an
SQL COPY command, but instead of the server reading or writing the
specified file, psql reads or writes the file and routes the data
between the server and the local file system. This means that file
accessibility and privileges are those of the local user, not the
server, and no SQL superuser privileges are required.
Issue this command on PgAdmin or psql:
COPY (SELECT * FROM persons) to 'C:\\tmp\\persons_client.csv' with csv;
Don't forget to escape Windows file separator.
\COPY (SELECT * FROM persons) to 'persons_client.csv' with csv;

mass import .csv files into postgresql

i am using Postgresql 9.4 and trying to import all files in a specific folder into and existing table using the following command:
COPY xxx_table FROM '/filepath/filename_*.csv' DELIMITER ',' CSV HEADER;
marking * as a variable part of the file name.
However it results in an error. I have found similar question on here however non of them is related to "COPY" command or alternatively using psql.
Any help on this would be highly appriciated.
Do you need first create the table manually, then:
Copy data from your CSV file to the table:
Example:
\copy zip_codes FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV
You can also specify the columns to read:
Example:
\copy zip_codes(ZIP,CITY,STATE) FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV

Importing CSV file into PostgreSQL

Using MySQL Administrator GUI tool I have exported some data tables retrieved from an sql dumpfile to csv files.
I then tried to import these CSV files into a PostgreSQL database using the postgres COPY command. I've tried entering
COPY articles FROM '[insert .csv dir here]' DELIMITERS ',' CSV;
and also the same command without the delimiters part.
I get an error saying
ERROR: invalid input syntax for integer: "id"
CONTEXT: COPY articles, line 1, column id: "id"
In conclusion my question is what are some thoughts and solutions to this problem? Could it possibly be something to do with the way I created the csv files? or have I made a rookie mistake elsewhere?
If you have header columns just add the header qualifier to the copy statement as per
documentation to skip that line
http://www.postgresql.org/docs/8.4/static/sql-copy.html