From: | Sigurdur Gunnlaugsson <sig(at)fjolnet(dot)net> |
---|---|
To: | Assad Jarrahian <jarraa(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Connect to a database in a .sql file |
Date: | 2005-11-08 18:29:07 |
Message-ID: | 1131474547.4523.8.camel@raftahlid35 |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Tue, 2005-11-08 at 07:31, Assad Jarrahian wrote:
> Hi,
> Lets say the script is called myDBSetup.sql
>
> And the script contains:
>
> //CREATE DATABASE section
> //CREATE USERS SECTION
> //COnnect to db
> //CREATE TABLES, FUNCTIONS etc.
>
> this script will be called from psql. The user will log connect to
> template1 and then run my script. What I really need is after the
> CREATE DB, I need to switch from template1 to the database name (so
> the CREATE tables etc will correspond to the right db). This has to be
> automated and done within the script.
>
> \c does not work in .sql script run in psql.
>
> Any suggestions would be helpful. Thanks.
> -assad
>
As others have mentioned \c works from sql script, small example follows
-- test.sql
CREATE database test3;
\c test3
CREATE TABLE test_table (
field1 integer,
field2 varchar(10));
INSERT INTO test_table (field1, field2) VALUES (1,'VALUE1');
INSERT INTO test_table (field1, field2) VALUES (2,'VALUE2');
SELECT * FROM test_table;
-- Cut
[post800b1(at)raftahlid35 projects]$ psql -a -f test.sql template1
CREATE database test3;
CREATE DATABASE
\c test3
You are now connected to database "test3".
CREATE TABLE test_table (
field1 integer,
field2 varchar(10));
CREATE TABLE
INSERT INTO test_table (field1, field2) VALUES (1,'VALUE1');
INSERT 25513 1
INSERT INTO test_table (field1, field2) VALUES (2,'VALUE2');
INSERT 25514 1
SELECT * FROM test_table;
field1 | field2
--------+--------
1 | VALUE1
2 | VALUE2
(2 rows)
--
Sigurdur
From | Date | Subject | |
---|---|---|---|
Next Message | Vivek Khera | 2005-11-08 18:41:56 | Re: Perl::DBI and interval syntax |
Previous Message | Joe Lester | 2005-11-08 18:25:43 | Re: Detect Locked Row Without Blocking |