How to "use" database?

Started by Anton Andreevabout 18 years ago5 messagesgeneral
Jump to latest
#1Anton Andreev
anton.andreev@fmi.uni-sofia.bg

Hi,

How to use a database I have just created in a script that I am
executing in Pgadmin3 on Windows?
I can not use "USE Northwind;" or "\connect Northwind;"?

Cheers,
Anton

#2Andrej Ricnik-Bay
andrej.groups@gmail.com
In reply to: Anton Andreev (#1)
Re: How to "use" database?

On 25/03/2008, Anton Andreev <anton.andreev@fmi.uni-sofia.bg> wrote:

Hi,

Hi,

How to use a database I have just created in a script that I am
executing in Pgadmin3 on Windows?
I can not use "USE Northwind;" or "\connect Northwind;"?

Hard to say w/o knowing the script. Does it just create tables,
or does it supposedly create a database as well?

Cheers,
Anton

Cheers,
Andrej

--
Please don't top post, and don't use HTML e-Mail :} Make your quotes concise.

http://www.american.edu/econ/notes/htmlmail.htm

#3Shane Ambler
pgsql@Sheeky.Biz
In reply to: Anton Andreev (#1)
Re: How to "use" database?

Anton Andreev wrote:

Hi,

How to use a database I have just created in a script that I am
executing in Pgadmin3 on Windows?

If you are in pgAdmin then you don't need the connect command. When you
select the db from the side list it connects you and lists the contents
of the db.
From the query window in pgAdmin (where you can enter any sql
statements) which is where I think you are opening your script file, it
will have a popup menu that you can choose the db to run the commands on
(or the sql script you are opening).

I can not use "USE Northwind;" or "\connect Northwind;"?

I believe you get the USE command from MS SQL Server examples - postgres
doesn't have that command.

The \connect is a 'shell' command used in psql - the cli based postgres
client that comes with postgresql. This would not be recognised in the
pgAdmin query window, which would only accept valid SQL statements.

I am thinking that if you are writing a script that creates a db then
wants to connect to it and create tables etc, you should look at sending
it to psql.

From a dos prompt you can use something like -
psql -U postgres -W -f myscript.sql

If you start psql then you can use the psql command \i myscript.sql to
run the script.

You may need to add full pathnames in these examples.

--

Shane Ambler
pgSQL (at) Sheeky (dot) Biz

Get Sheeky @ http://Sheeky.Biz

#4Anton Andreev
anton.andreev@fmi.uni-sofia.bg
In reply to: Shane Ambler (#3)
Re: How to "use" database?

Hi,

Thank you for you detailed answer, but I am exactly in the case when I
want to paste the script and hit F5 and have both the database and
tables created.

A problem also comes from the fact that in order to use the query window
in pgadmin3, you need to select a database. Then when you create a new
database you can not access it by name cause you receive an error "cross
database referencing is not allowed".

Cheers,
Anton

Shane Ambler wrote:

Show quoted text

Anton Andreev wrote:

Hi,

How to use a database I have just created in a script that I am
executing in Pgadmin3 on Windows?

If you are in pgAdmin then you don't need the connect command. When
you select the db from the side list it connects you and lists the
contents of the db.
From the query window in pgAdmin (where you can enter any sql
statements) which is where I think you are opening your script file,
it will have a popup menu that you can choose the db to run the
commands on (or the sql script you are opening).

I can not use "USE Northwind;" or "\connect Northwind;"?

I believe you get the USE command from MS SQL Server examples -
postgres doesn't have that command.

The \connect is a 'shell' command used in psql - the cli based
postgres client that comes with postgresql. This would not be
recognised in the pgAdmin query window, which would only accept valid
SQL statements.

I am thinking that if you are writing a script that creates a db then
wants to connect to it and create tables etc, you should look at
sending it to psql.

From a dos prompt you can use something like -
psql -U postgres -W -f myscript.sql

If you start psql then you can use the psql command \i myscript.sql to
run the script.

You may need to add full pathnames in these examples.

#5Shane Ambler
pgsql@Sheeky.Biz
In reply to: Anton Andreev (#4)
Re: How to "use" database?

Anton Andreev wrote:

Hi,

Thank you for you detailed answer, but I am exactly in the case when I
want to paste the script and hit F5 and have both the database and
tables created.

A problem also comes from the fact that in order to use the query window
in pgadmin3, you need to select a database. Then when you create a new
database you can not access it by name cause you receive an error "cross
database referencing is not allowed".

You may be able to get your script to work with pgAdmin's restore option
(which seems to use pg_dump)

Try opening your script using the restore command in pgAdmin (Tools menu).

If it doesn't work, backup from pgadmin as a plain file and compare what
it puts out to what you have done.

--

Shane Ambler
pgSQL (at) Sheeky (dot) Biz

Get Sheeky @ http://Sheeky.Biz