pgsql -c in bash shell script quote problem - postgresql
This is a discussion on pgsql -c in bash shell script quote problem - postgresql ; Hi, I want to automate a task to copy values to a table. The shell script is this (simple form): psql -U postgres -d sedait_db -c ' COPY dp_calc_temp ( ano ) FROM \'/usr/local/pgsql/home/dw/iva_dp/calculados/ dp_calculados_juntos.csv\' WITH CSV ; ' The ...
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| I want to automate a task to copy values to a table. The shell script is this (simple form): psql -U postgres -d sedait_db -c ' COPY dp_calc_temp ( ano ) FROM \'/usr/local/pgsql/home/dw/iva_dp/calculados/ dp_calculados_juntos.csv\' WITH CSV ; ' The problem is that I have to use ' to delimit the entire psql command, but ' is also required to for the file reference. I tried to escape ' with \', as in the example, but I get as a response: unexpected EOF while liiking for matching `'' syntax error: unexpected end of file If I try no \ escaping, I get a syntax error near "/" Also tried do use double ', but to no avail. I even tried $/usr/...$, but didn't make it. How can this be solved? Luis |
|
#2
| |||
| |||
|
On 2007-09-05, luislupe@gmail.com > Hi, > > I want to automate a task to copy values to a table. > > The shell script is this (simple form): > psql -U postgres -d sedait_db -c ' > COPY dp_calc_temp > ( > ano > ) > FROM \'/usr/local/pgsql/home/dw/iva_dp/calculados/ > dp_calculados_juntos.csv\' WITH CSV ; ' > > The problem is that I have to use ' to delimit the entire psql > command, but ' is also required to for the file reference. > > I tried to escape ' with \', as in the example, but I get as a > response: > unexpected EOF while liiking for matching `'' > syntax error: unexpected end of file > > If I try no \ escaping, I get a syntax error near "/" > > Also tried do use double ', but to no avail. > > I even tried $/usr/...$, but didn't make it. > > How can this be solved? > > > Luis > Hi, write your script in file and then psql .... -f file rgds, d. |
|
#3
| |||
| |||
|
On Sep 5, 3:56 pm, sircco > On 2007-09-05, luisl...@gmail.com > > > Hi, > > > I want to automate a task to copy values to a table. > > > The shell script is this (simple form): > > psql -U postgres -d sedait_db -c ' > > COPY dp_calc_temp > > ( > > ano > > ) > > FROM \'/usr/local/pgsql/home/dw/iva_dp/calculados/ > > dp_calculados_juntos.csv\' WITH CSV ; ' > > > The problem is that I have to use ' to delimit the entire psql > > command, but ' is also required to for the file reference. > > > I tried to escape ' with \', as in the example, but I get as a > > response: > > unexpected EOF while liiking for matching `'' > > syntax error: unexpected end of file > > > If I try no \ escaping, I get a syntax error near "/" > > > Also tried do use double ', but to no avail. > > > I even tried $/usr/...$, but didn't make it. > > > How can this be solved? > > > Luis > > Hi, > > write your script in file and then psql .... -f file > > rgds, > d. Thank you! You saved my day, although this way there is some flexibility lost, for example in assigning variable names to files. Luis |
|
#4
| |||
| |||
| luislupe@gmail.com > I want to automate a task to copy values to a table. > > The shell script is this (simple form): > psql -U postgres -d sedait_db -c ' > COPY dp_calc_temp > ( > ano > ) > FROM \'/usr/local/pgsql/home/dw/iva_dp/calculados/ > dp_calculados_juntos.csv\' WITH CSV ; ' > > The problem is that I have to use ' to delimit the entire psql > command, but ' is also required to for the file reference. > > I tried to escape ' with \', as in the example, but I get as a > response: > unexpected EOF while liiking for matching `'' > syntax error: unexpected end of file > > If I try no \ escaping, I get a syntax error near "/" > > Also tried do use double ', but to no avail. > > I even tried $/usr/...$, but didn't make it. What keeps you from using double quotes? psql ... -c "COPY .... FROM 'filename'" Yours, Laurenz Albe |
|
#5
| |||
| |||
| luislupe@gmail.com wrote: > I tried to escape ' with \', as in the example, but I get as a > response: > unexpected EOF while liiking for matching `'' > syntax error: unexpected end of file You can only escape and use shell variables between double quotes, not between single quotes. Best to use double quotes anyway, |
|
#6
| |||
| |||
|
I was trying to use this on a shell script on ubuntu. my objective is to read all CSV file on a certain directory and load its contents on the specified table on the "Copy from " statement. this raise an error Syntax error at or near ":" psql -c "copy sometable from om _filename WITH HEADER CSV;" -e -t -v p_filename=$CSV_FILE -h xx.xx.xx.xx -p 54444 -U someuser dbname=somedbcan anyone suggestion why " _filename" cannot be use hereOr do i use it right? do we have alternate solutions here" |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
All times are GMT -4. The time now is 11:52 AM.




_filename WITH HEADER CSV;" -e -t -v p_filename=$CSV_FILE -h xx.xx.xx.xx -p 54444 -U someuser dbname=somedb
Linear Mode