Unix Technical Forum

multiple psql commands in batch

This is a discussion on multiple psql commands in batch within the pgsql Novice forums, part of the PostgreSQL category; --> G'Day all, i know that this issues was discussed multiple time, but i still stuck in processing multiple pgsql ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:55 PM
Tobias Boes
 
Posts: n/a
Default multiple psql commands in batch

G'Day all,

i know that this issues was discussed multiple time, but i still stuck in processing multiple pgsql commands in a DOS batch file.
I want to create a user, than a database with the following command:
C:\financial-planner\pgsql\bin\psql.exe -U postgres < create_user_and_db.sql

this scceeds. in the create_user_and_db.sql I stored the SQL-commands to setup the database and the database user.
but afterwards I want to switch to "mydatabase" and process the create table statements. In the pgsql shell I can switch to "mydatabase" with the following command:
\c mydatabase
but how can I run mulitple pgsql commands in a batch job? After loads of reading I'm still not able to do this.
Any help is really appreciated!
Thanks,
Tobias
--


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:55 PM
Daniel T. Staal
 
Posts: n/a
Default Re: multiple psql commands in batch

On Fri, June 2, 2006 2:28 pm, Tobias Boes said:
> G'Day all,
>
> i know that this issues was discussed multiple time, but i still stuck in
> processing multiple pgsql commands in a DOS batch file.
> I want to create a user, than a database with the following command:
> C:\financial-planner\pgsql\bin\psql.exe -U postgres <
> create_user_and_db.sql
>
> this scceeds. in the create_user_and_db.sql I stored the SQL-commands to
> setup the database and the database user.
> but afterwards I want to switch to "mydatabase" and process the create
> table statements. In the pgsql shell I can switch to "mydatabase" with the
> following command:
> \c mydatabase
> but how can I run mulitple pgsql commands in a batch job? After loads of
> reading I'm still not able to do this.
> Any help is really appreciated!
> Thanks,
> Tobias


The switch you are looking for is '-d'. Use like such:
C:\financial-planner\pgsql\bin\psql.exe -d mydatabase

(With appropriate user and commands, of course.)

I'd move the create table statements to a seperate file, and make another
call from the batch file.

(Otherwise, in this context I believe schema == database, so if you
appended the database name to table names in the SQL creates like this
'mydatabase.mytable' that should work as well.)

Daniel T. Staal

---------------------------------------------------------------
This email copyright the author. Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes. This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:55 PM
Andrej Ricnik-Bay
 
Posts: n/a
Default Re: multiple psql commands in batch

Something like:

create database
\connect database user
create table ....



Cheers,
Andrej

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 10:55 PM
Tobias Boes
 
Posts: n/a
Default Re: multiple psql commands in batch

ok, but when I have a batch file like this:
>>>

@echo off

C:\postgresdir\pgsql\bin\psql.exe -U postgres
CREATE DATABASE mydatabase;
CREATE USER mydbuser PASSWORD 'mypwd';
<<<

i can't get it to run. How do i set SQL-commands in this case?

> -------- Original-Nachricht --------
> Datum: Sat, 3 Jun 2006 10:48:57 +1200
> Von: Andrej Ricnik-Bay <andrej.groups@gmail.com>
> An: Tobias Boes <tobias.boes@gmx.de>
> Betreff: Re: [NOVICE] multiple psql commands in batch
>
> Something like:
>
> create database
> \connect database user
> create table ....
>
> Cheers,
> Andrej
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org


--


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com