+ Reply to Thread
Results 1 to 6 of 6

pgsql -c in bash shell script quote problem

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


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

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


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

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

  6. 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 to Thread