Re: SQL from Linux command line - Mailing list pgsql-novice

From Andreas Kretschmer
Subject Re: SQL from Linux command line
Date
Msg-id 20100710064237.GA9921@tux
Whole thread Raw
In response to SQL from Linux command line  (Scott Geller <geller.subscribe@gmail.com>)
Responses Re: SQL from Linux command line
List 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 by date:

Previous
From: Siddharth Saha
Date:
Subject: how to change line terminator
Next
From: "Matthew Sleight"
Date:
Subject: