Re: Insert psql commands inside a script - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: Insert psql commands inside a script
Date
Msg-id 20050423055055.GB96734@winnie.fuhr.org
Whole thread Raw
In response to Insert psql commands inside a script  ("Letnes, David G." <david.letnes@unisys.com>)
List 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/


pgsql-sql by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: Coming from Oracle SQL
Next
From: Andreas Kretschmer
Date:
Subject: Re: [despammed] Insert psql commands inside a script