Re: [SQL] Re: Query is executing but giving error when using withshell scripting - Mailing list pgsql-sql

From Feike Steenbergen
Subject Re: [SQL] Re: Query is executing but giving error when using withshell scripting
Date
Msg-id CAK_s-G1xOP5rcpZUcwTPhA+rCu=CWnqdPwA8L=Qcm5ezBhj+uA@mail.gmail.com
Whole thread Raw
In response to [SQL] Re: Query is executing but giving error when using with shellscripting  (srilinux <srilinux09@gmail.com>)
List pgsql-sql


On 14 September 2017 at 17:58, srilinux <srilinux09@gmail.com> wrote:
> coba1=# select name from users where "Date" >current_date - 30;
>         name
> ---------------------
>  coba11
>  testCoba11
>



On 14 September 2017 at 18:27, srilinux <srilinux09@gmail.com> wrote:
>
> Did not help
>
> /usr/bin/psql -U postgres  -d coba1 -c  "select name from users  where Date
> > current_date - 30;"`
>
> same error
>

It looks like you have a column named Date, capitalized. Which means you actually need to surround your column name with double quotes.

Your shell (bash?) is removing the double quotes, so either escape your double quotes:

/usr/bin/psql -U postgres  -d coba1 -c  "select name from users where
\"Date\" > current_date - 30;"

Or use single quotes to surround the query, which also prevents your shell from removing the double quotes.

/usr/bin/psql -U postgres  -d coba1 -c  'select name from users where
"Date" > current_date - 30;'

You may also want to consider not using capitalized column names, as it would remove the need to quote column names altogether.

pgsql-sql by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: [SQL] Static variable inside PL/pgSQL (or) native C function
Next
From: Gourav Kumar
Date:
Subject: [SQL] Get relation name form relids in postgresql