Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass variable to copy command in Postgresql

I tried to make a variable in SQL statement in Postgresql, but it did not work. There are many csv files stored under the path. I want to set path in Postgresql that can tell copy command where can find csv files.

SQL statement sample:

\set outpath '/home/clients/ats-dev/'
\COPY licenses (_id, name,number_seats ) FROM :outpath + 'licenses.csv' CSV HEADER DELIMITER ',';
\COPY uploaded_files (_id, added_date ) FROM :outpath + 'files.csv' CSV HEADER DELIMITER ',';

It did not work. I got error: no such files. The two files licneses.csv and files.csv are stored under /home/cilents/ats-dev on Ubuntu. I found some sultion that use "\set file 'license.csv'". It did not work for me becacuse I have many csv files. also I tried to use "from : outpath || 'licenses.csv'". it did not work ether. Appreciate for any helps.

Using 9.3.

like image 631
user7070824 Avatar asked Jul 27 '17 16:07

user7070824


People also ask

How do I use Copy command in PostgreSQL?

PSQL \Copy Command for Client-Side Export To 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.

What does the Copy command in PostgreSQL do?

COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already). COPY TO can also copy the results of a SELECT query.

How do I SELECT a variable in PostgreSQL?

In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.

Can you use variables in PostgreSQL?

PL/pgSQL variables can have any SQL data type, such as integer , varchar , and char . Here are some examples of variable declarations: user_id integer; quantity numeric(5); url varchar; myrow tablename%ROWTYPE; myfield tablename.


2 Answers

It looks like psql does not support :variable substitution withinpsql backslash commands.

test=> \set somevar fred
test=> \copy z from :somevar
:somevar: No such file or directory

so you will need to do this via an external tool like the unix shell. e.g.

for f in *.sql; do
    psql -c "\\copy $(basename $f) FROM '$f'"
done
like image 159
Craig Ringer Avatar answered Nov 06 '22 09:11

Craig Ringer


You can try COPY command

\set outpath '\'/home/clients/ats-dev/'
COPY licenses (_id, name,number_seats ) FROM :outpath/licenses.csv' WITH CSV HEADER DELIMITER ',';
COPY uploaded_files (_id, added_date ) FROM :outpath/files.csv' WITH CSV HEADER DELIMITER ',';

Note: Files named in a COPY command are read or written directly by the server, not by the client application. Therefore, they must reside on or be accessible to the database server machine, not the client. They must be accessible to and readable or writable by the PostgreSQL user (the user ID the server runs as), not the client. Similarly, the command specified with PROGRAM is executed directly by the server, not by the client application, must be executable by the PostgreSQL user. COPY naming a file or command is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.

Documentation: Postgresql 9.3 COPY

like image 42
Anurag Paul Avatar answered Nov 06 '22 09:11

Anurag Paul