Обсуждение: [SQL] Query is executing but giving error when using with shell scripting

Поиск
Список
Период
Сортировка

[SQL] Query is executing but giving error when using with shell scripting

От
srilinux
Дата:
Please suggest what is giving error here
when I execute from psql prompt, I get the result, when  I try to automate
using a shell script, the query is not working

# `/usr/bin/psql -U postgres  -d coba1 -c  "select name from users where
"Date" > current_date - 30;"`
ERROR:  column "Date" does not exist
LINE 1: select name from users where Date >current_...


========

works below
                                         ^sudo -u postgres psql

psql (9.4.5)
Type "help" for help.

postgres=# \connect coba1
You are now connected to database "coba1" as user "postgres".
coba1=# select name from users where "Date" >current_date - 30;       name
---------------------coba11testCoba11





--
Sent from: http://www.postgresql-archive.org/PostgreSQL-sql-f2142323.html


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Re: [SQL] Query is executing but giving error when using with shell scripting

От
Andreas Kretschmer
Дата:
On 14 September 2017 17:58:07 GMT+02:00, srilinux <srilinux09@gmail.com> wrote:
>Please suggest what is giving error here
>when I execute from psql prompt, I get the result, when  I try to
>automate
>using a shell script, the query is not working
>
># `/usr/bin/psql -U postgres  -d coba1 -c  "select name from users
>where
>"Date" > current_date - 30;"`
>ERROR:  column "Date" does not exist
>LINE 1: select name from users where Date >current_...
>
>
>========
>Remove the " from Date, or write "date". It is just a quoting-problem.


Regards, Andreas

--
2ndQuadrant - The PostgreSQL Support Company


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

[SQL] Re: Query is executing but giving error when using with shellscripting

От
srilinux
Дата:
Did not help 

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

same error 



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-sql-f2142323.html


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Re: [SQL] Re: Query is executing but giving error when using withshell scripting

От
Feike Steenbergen
Дата:


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.