Re: Insert psql commands inside a script

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "Letnes, David G(dot)" <david(dot)letnes(at)unisys(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Insert psql commands inside a script
Date: 2005-04-23 05:50:55
Message-ID: 20050423055055.GB96734@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, Apr 21, 2005 at 05:19:23PM -0500, Letnes, David G. wrote:
>
> I have used the psql -f /tmp/SelectCommands.sql before, but now I want
> to put the sql statement right in the shell script. I haven't had any
> luck. Is there a command I can use that will not point to a file for
> the sql instructions but right on the same line. I use very short psql
> commands and would like to do it all with 1 file.

For simple queries you can use psql -c:

psql -c 'SELECT * FROM foo'

You can embed an SQL script with a "here document" if your shell
supports it (it probably does):

#!/bin/sh

echo "before database connection"

psql <<END_OF_SQL
CREATE TABLE foo (x integer);
INSERT INTO foo VALUES (42);
SELECT * FROM foo;
DROP TABLE foo;
END_OF_SQL

echo "after database connection"

See your shell's documentation for details. See also the psql
documentation for its options and ways to control its output.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Kretschmer 2005-04-23 06:05:38 Re: [despammed] Insert psql commands inside a script
Previous Message Michael Fuhr 2005-04-23 05:29:59 Re: Coming from Oracle SQL