Thread: Insert psql commands inside a script
<br /><p><font face="Arial" size="2"> I have used the psql -f /tmp/SelectCommands.sql before, but now I want to put thesql statement right in the shell script. I haven't had any luck. Is there a command I can use that will not point toa file for the sql instructions but right on the same line. I use very short psql commands and would like to do it allwith 1 file.</font><p><font face="Arial" size="2">Thank You</font><br /><font face="Arial" size="2">David</font><ul><p><b><fontcolor="#000000" face="Arial" size="1">THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OROTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, pleasecontact the sender and delete the e-mail and its attachments from all computers. </font></b><br /></ul>
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/
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 ===
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
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/
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