Re: SQL from Linux command line

Поиск
Список
Период
Сортировка
От Andreas Kretschmer
Тема Re: SQL from Linux command line
Дата
Msg-id 20100710064237.GA9921@tux
обсуждение исходный текст
Ответ на SQL from Linux command line  (Scott Geller <geller.subscribe@gmail.com>)
Ответы Re: SQL from Linux command line  (Alan Hodgson <ahodgson@simkin.ca>)
Список pgsql-novice
Scott Geller <geller.subscribe@gmail.com> wrote:

>
> Hello
>
> Hoping for some help on this one....
>
> I need to query postgres, run a count of specific records, and write to a
> different table based on the results all from the linux command line.
>
> My questions are:
>
>  1. Would I use psql for this?
>  2. If so, how do I structure the script to run psql from Linux?
>  3. how do I pass the value for the where clause from the command line into the
>     code?

Sure, you can use psql for that, here a simple example:


,----[  my script  ]
| kretschmer@tux:~$ cat test.sh
| #!/bin/bash
|
| d=`date`
|
| psql test -c "select 'today is ' || '$d' || ', and the database says ' || now()"
`----

,----[  running the script  ]
| kretschmer@tux:~$ ./test.sh
|                                           ?column?
| ---------------------------------------------------------------------------------------------
|  today is Sa 10. Jul 08:40:25 CEST 2010, and the database says 2010-07-10 08:40:25.248636+02
| (1 Zeile)
|
| kretschmer@tux:~$
`----


You got it?


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

В списке pgsql-novice по дате отправления:

Предыдущее
От: Siddharth Saha
Дата:
Сообщение: how to change line terminator
Следующее
От: "Matthew Sleight"
Дата:
Сообщение: