Re: SQL from Linux command line

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: SQL from Linux command line
Date: 2010-07-10 06:42:37
Message-ID: 20100710064237.GA9921@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Scott Geller <geller(dot)subscribe(at)gmail(dot)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(at)tux:~$ cat test.sh
| #!/bin/bash
|
| d=`date`
|
| psql test -c "select 'today is ' || '$d' || ', and the database says ' || now()"
`----

,----[ running the script ]
| kretschmer(at)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(at)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°

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Mladen Gogala 2010-07-10 12:03:16 Re: SQL from Linux command line
Previous Message Scott Geller 2010-07-10 05:25:16 SQL from Linux command line