Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: \copy syntax

Tags:

postgresql

With PostgreSQL 9.5 on CentOS 7, I have created a database named sample along with several tables. I have .csv data in /home/MyUser/data for each table. For example, there exists TableName.csv for the table "TableName".

How do I load the csv files into each table?


What I've tried doesn't work and I can't figure out what I'm doing wrong.

Load from within the DB

$ psql sample

sample=# COPY "TableName" FROM '/home/MyUser/data/TableName.csv' WITH CSV;

ERROR:  could not open file "/home/MyUser/data/TableName.csv" for reading: Permission denied

This implies a file permission problem. All the files in data/ are -rw-r--r-- and the directory itself is drwxr-xr-x. So file permissions shouldn't be the problem (unless I'm missing something). The internet says that COPY has problems with permissions and to try \copy.

Load from CLI

$ psql \copy sample FROM /home/MyUser/data/TableName.csv WITH CSV

psql: warning: extra command-line argument "FROM" ignored
psql: warning: extra command-line argument "/home/MyUser/data/TableName.csv" ignored
psql: warning: extra command-line argument "WITH" ignored
psql: warning: extra command-line argument "CSV" ignored
psql: FATAL:  Peer authentication failed for user "sample"

This appears to be a syntax error, but I'm not finding the documentation particularly helpful (man psql then /\copy). I've also tried the following to the same result.

$ psql \copy sample."TableName" FROM /home/MyUser/data/TableName.csv WITH CSV
$ psql \copy sample FROM /home/MyUser/data/TableName.csv WITH DELIMITER ','

There are several other permutations which yield similar errors.

Web Resources Used

  • https://www.postgresql.org/docs/9.5/static/app-psql.html

  • https://www.postgresql.org/docs/9.5/static/sql-copy.html

  • The correct COPY command to load postgreSQL data from csv file that has single-quoted data?

  • https://soleil4716.wordpress.com/2010/08/19/using-copy-command-in-postgresql/

  • Can I use \copy command into a function of postgresql?

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

like image 607
Lorem Ipsum Avatar asked Feb 19 '18 19:02

Lorem Ipsum


People also ask

How do I use Copy command in PostgreSQL?

PSQL \Copy Command for Client-Side ExportTo copy the entire table to a csv file, use \copy. This will copy the contents of a table to the client computer as a csv file. The file will not contain the headers of the table. \copy employees to '/var/lib/postgresql/emp.

How do I COPY output from PostgreSQL?

The easiest but the most efficient way to export data from a Postgres table to a CSV file is by using the COPY command. COPY command generates a CSV file on the Database Server. You can export the entire table or the results of a query to a CSV file with the COPY TO command.

How do I COPY a table in PostgreSQL?

To copy a table with partial data from an existing table, users can use the following statement: Syntax: CREATE TABLE new_table AS SELECT * FROM existing_table WHERE condition; The condition in the WHERE clause of the query defines which rows of the existing table will be copied to the new table.


1 Answers

About the permissions:

Don't forget that to access a file you need permissions on all directories in the path. So if, for example, the OS user postgres does not have permissions on the /home/MyUser directory, you get the observed error message.

About \copy:

You have to use the -c option to supply a command to psql:

$ psql -c "\copy sample FROM '/home/MyUser/data/TableName.csv' WITH (FORMAT CSV)"
like image 161
Laurenz Albe Avatar answered Sep 21 '22 02:09

Laurenz Albe