pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Insert psql commands inside a script
6+ messages / 4 participants
[nested] [flat]

* Insert psql commands inside a script
@ 2005-04-21 22:19 Letnes, David G. <david.letnes@unisys.com>
  2005-04-23 05:50 ` Re: Insert psql commands inside a script Michael Fuhr <mike@fuhr.org>
  2005-04-23 06:05 ` Re: [despammed] Insert psql commands inside a script Andreas Kretschmer <akretschmer@despammed.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Letnes, David G. @ 2005-04-21 22:19 UTC (permalink / raw)
  To: pgsql-sql


  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.

Thank You
David

> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE
> PROPRIETARY MATERIAL and is thus for use only by the intended
> recipient. If you received this in error, please contact the sender
> and delete the e-mail and its attachments from all computers. 
> 
> 

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Insert psql commands inside a script
  2005-04-21 22:19 Insert psql commands inside a script Letnes, David G. <david.letnes@unisys.com>
@ 2005-04-23 05:50 ` Michael Fuhr <mike@fuhr.org>
  1 sibling, 0 replies; 6+ messages in thread

From: Michael Fuhr @ 2005-04-23 05:50 UTC (permalink / raw)
  To: Letnes, David G. <david.letnes@unisys.com>; +Cc: 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/



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [despammed] Insert psql commands inside a script
  2005-04-21 22:19 Insert psql commands inside a script Letnes, David G. <david.letnes@unisys.com>
@ 2005-04-23 06:05 ` Andreas Kretschmer <akretschmer@despammed.com>
  2005-04-23 12:15   ` Re: [despammed] Insert psql commands inside a script Geoffrey <esoteric@3times25.net>
  1 sibling, 1 reply; 6+ messages in thread

From: Andreas Kretschmer @ 2005-04-23 06:05 UTC (permalink / raw)
  To: pgsql-sql

am  21.04.2005, um 17:19:23 -0500 mailte Letnes, David G. folgendes:
> 
>   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.

echo "select bla from fasel" | psql -U <username> database

You can also use here documents, shell-variables and so on.


Regards, Andreas
-- 
Andreas Kretschmer    (Kontakt: siehe Header)
Heynitz:  035242/47212,      D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
 ===    Schollglas Unternehmensgruppe    === 



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [despammed] Insert psql commands inside a script
  2005-04-21 22:19 Insert psql commands inside a script Letnes, David G. <david.letnes@unisys.com>
  2005-04-23 06:05 ` Re: [despammed] Insert psql commands inside a script Andreas Kretschmer <akretschmer@despammed.com>
@ 2005-04-23 12:15   ` Geoffrey <esoteric@3times25.net>
  2005-04-23 13:30     ` Re: [despammed] Insert psql commands inside a script Michael Fuhr <mike@fuhr.org>
  0 siblings, 1 reply; 6+ messages in thread

From: Geoffrey @ 2005-04-23 12:15 UTC (permalink / raw)
  To: pgsql-sql

Andreas Kretschmer wrote:
> am  21.04.2005, um 17:19:23 -0500 mailte Letnes, David G. folgendes:
> 
>>  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.
> 
> 
> echo "select bla from fasel" | psql -U <username> database
> 
> You can also use here documents, shell-variables and so on.

Or you can do the following:

psql database < sqlcode

where sqlcode contains your sql.

-- 
Until later, Geoffrey



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [despammed] Insert psql commands inside a script
  2005-04-21 22:19 Insert psql commands inside a script Letnes, David G. <david.letnes@unisys.com>
  2005-04-23 06:05 ` Re: [despammed] Insert psql commands inside a script Andreas Kretschmer <akretschmer@despammed.com>
  2005-04-23 12:15   ` Re: [despammed] Insert psql commands inside a script Geoffrey <esoteric@3times25.net>
@ 2005-04-23 13:30     ` Michael Fuhr <mike@fuhr.org>
  2005-04-23 13:46       ` Re: [despammed] Insert psql commands inside a script Geoffrey <esoteric@3times25.net>
  0 siblings, 1 reply; 6+ messages in thread

From: Michael Fuhr @ 2005-04-23 13:30 UTC (permalink / raw)
  To: Geoffrey <esoteric@3times25.net>; +Cc: pgsql-sql

On Sat, Apr 23, 2005 at 08:15:48AM -0400, Geoffrey wrote:
> 
> Or you can do the following:
> 
> psql database < sqlcode
> 
> where sqlcode contains your sql.

That command redirects the standard input from the file named
sqlcode; the requirement says NOT to use an external file.

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [despammed] Insert psql commands inside a script
  2005-04-21 22:19 Insert psql commands inside a script Letnes, David G. <david.letnes@unisys.com>
  2005-04-23 06:05 ` Re: [despammed] Insert psql commands inside a script Andreas Kretschmer <akretschmer@despammed.com>
  2005-04-23 12:15   ` Re: [despammed] Insert psql commands inside a script Geoffrey <esoteric@3times25.net>
  2005-04-23 13:30     ` Re: [despammed] Insert psql commands inside a script Michael Fuhr <mike@fuhr.org>
@ 2005-04-23 13:46       ` Geoffrey <esoteric@3times25.net>
  0 siblings, 0 replies; 6+ messages in thread

From: Geoffrey @ 2005-04-23 13:46 UTC (permalink / raw)
  To: pgsql-sql

Michael Fuhr wrote:
> On Sat, Apr 23, 2005 at 08:15:48AM -0400, Geoffrey wrote:
> 
>>Or you can do the following:
>>
>>psql database < sqlcode
>>
>>where sqlcode contains your sql.
> 
> 
> That command redirects the standard input from the file named
> sqlcode; the requirement says NOT to use an external file.

My bad, I misread the post as indicating he wanted to use an external 
file.  Too early on a Saturday with a late Friday and not enough coffee 
yet...

-- 
Until later, Geoffrey



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2005-04-23 13:46 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-21 22:19 Insert psql commands inside a script Letnes, David G. <david.letnes@unisys.com>
2005-04-23 05:50 ` Michael Fuhr <mike@fuhr.org>
2005-04-23 06:05 ` Andreas Kretschmer <akretschmer@despammed.com>
2005-04-23 12:15   ` Geoffrey <esoteric@3times25.net>
2005-04-23 13:30     ` Michael Fuhr <mike@fuhr.org>
2005-04-23 13:46       ` Geoffrey <esoteric@3times25.net>

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