dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

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 ...


Home > Database Forum > Other Databases > postgresql > pgsql -c in bash shell script quote problem

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 09-05-2007, 10:36 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default pgsql -c in bash shell script quote problem

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

Reply With Quote
  #2  
Old 09-05-2007, 10:57 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: pgsql -c in bash shell script quote problem

On 2007-09-05, luislupe@gmail.com wrote:
> 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.
Reply With Quote
  #3  
Old 09-05-2007, 11:30 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: pgsql -c in bash shell script quote problem

On Sep 5, 3:56 pm, sircco wrote:
> On 2007-09-05, luisl...@gmail.com wrote:
>
> > 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

Reply With Quote
  #4  
Old 09-05-2007, 12:22 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: pgsql -c in bash shell script quote problem

luislupe@gmail.com wrote:
> 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
Reply With Quote
  #5  
Old 10-12-2007, 10:40 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: pgsql -c in bash shell script quote problem

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,
Reply With Quote
  #6  
Old 09-01-2009, 05:09 AM
Database Newbie
 
Join Date: Sep 2009
Posts: 2
nutcrackers is on a distinguished road
Default Re: pgsql -c in bash shell script quote problem

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=somedb

can anyone suggestion why "_filename" cannot be use here

Or do i use it right? do we have alternate solutions here"
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 11:52 AM.