Executing script file

Started by Tumurbaatar S.almost 22 years ago3 messagesgeneral
Jump to latest
#1Tumurbaatar S.
tumurbaatar@datacom.mn

Hi!
I cannot imagine how to create db from a script file and execute
other DDL statements on this new db.
Currently I use 'psql -f file' to perform all needed DDL statements
except of create db. Because when I insert 'CREATE DATABASE'
at the beginning of the script, the server creates a db but following
commands like 'CREATE TABLE' and etc run on the current db, i.e.
not in context of newly created db. Any ideas?

P.S. Some RDBMS automatically switches to a newly created db
after 'CREATE DATABASE' and some other languages have command
like 'USE' to switch to a different db.

#2Chris Mair
list@1006.org
In reply to: Tumurbaatar S. (#1)
Re: Executing script file

Currently I use 'psql -f file' to perform all needed DDL statements
except of create db. Because when I insert 'CREATE DATABASE'
at the beginning of the script, the server creates a db but following
commands like 'CREATE TABLE' and etc run on the current db, i.e.
not in context of newly created db. Any ideas?

Use \c, like this:

\c your_new_database

Bye, Chris.

#3Richard Huxton
dev@archonet.com
In reply to: Tumurbaatar S. (#1)
Re: Executing script file

On Tuesday 13 April 2004 08:40, Tumurbaatar S. wrote:

Hi!
I cannot imagine how to create db from a script file and execute
other DDL statements on this new db.

P.S. Some RDBMS automatically switches to a newly created db
after 'CREATE DATABASE' and some other languages have command
like 'USE' to switch to a different db.

You want to look into the "\c" command available in psql. That reconnects to a
different db / user.

Worth doing a pg_dump (text format) and looking at its output (or pg_restore
to a text-file).

--
Richard Huxton
Archonet Ltd