Обсуждение: Q on requirement to connect to a specific DB

Поиск
Список
Период
Сортировка

Q on requirement to connect to a specific DB

От
Дата:


As far as I understand, one always has to connect to a specific
database even when what one wants to do does not have anything to do
with any existing database.  For example, suppose I just want to
create a new database.  As far as I know I must connect to *some*
database, whose role is irrelevant, just so that I can execute "CREATE
DATABASE ..."  from that connection.

I find this puzzling from a design perspective.  Why involve some
irrelevant DB in the process of creating a new one?  Am I
understanding the whole situation correctly?  If so, what is the
"canonical" database to connect to?  (Is this the purpose of
"template1"?)

Thanks!

kj


Re: Q on requirement to connect to a specific DB

От
Sean Davis
Дата:
kynn@panix.com wrote:
> As far as I understand, one always has to connect to a specific
> database even when what one wants to do does not have anything to do
> with any existing database.  For example, suppose I just want to
> create a new database.  As far as I know I must connect to *some*
> database, whose role is irrelevant, just so that I can execute "CREATE
> DATABASE ..."  from that connection.

You can also use the createdb program.

Sean

Re: Q on requirement to connect to a specific DB

От
Tom Lane
Дата:
<kynn@panix.com> writes:
> As far as I know I must connect to *some*
> database, whose role is irrelevant, just so that I can execute "CREATE
> DATABASE ..."  from that connection.

Correct.

> I find this puzzling from a design perspective.  Why involve some
> irrelevant DB in the process of creating a new one?

Because the backend can't operate without a set of system catalogs
to interrogate.  We could maybe have a dummy database that had no
other purpose than to be connected to for specific commands ...
oh wait, that's pretty much what we do have.

> If so, what is the "canonical" database to connect to?  (Is this the
> purpose of "template1"?)

Pre-8.1, that was the usual convention, but as of 8.1 we manufacture
an additional "standard" database named "postgres", and that's the
recommended connection target if you have no better alternative.
The reason for the change is that "template1" has the additional
function of being the default database-to-clone for CREATE DATABASE,
and there was too much conflict between those two uses.

            regards, tom lane