From: Michael Fuhr <mike@fuhr.org>
To: Letnes, David G. <david.letnes@unisys.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Insert psql commands inside a script
Date: Fri, 22 Apr 2005 23:50:55 -0600
Message-ID: <20050423055055.GB96734@winnie.fuhr.org> (raw)
In-Reply-To: <2F1DE8CC97C00645B9FEA539982DA22401900D5A@USEA-EXCH4.na.uis.unisys.com>
References: <2F1DE8CC97C00645B9FEA539982DA22401900D5A@USEA-EXCH4.na.uis.unisys.com>
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/
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: mike@fuhr.org, david.letnes@unisys.com
Subject: Re: Insert psql commands inside a script
In-Reply-To: <20050423055055.GB96734@winnie.fuhr.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox