如何在不指定数据库名称的情况下连接到PostgreSQL ?

时间:2021-01-11 07:33:30

I need to connect to some PostgreSQL server providing some credentials, and print a list of available databases on that host for a given user.

我需要连接到提供一些凭证的PostgreSQL服务器,并为给定用户在该主机上打印可用数据库列表。

I am trying:

我尝试:

<?php
    $connection = pg_connect("host=localhost user=testuser password=123 connect_timeout=5");
?>

And I get:

我得到:

Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: database "testuser" does not exist in /var/www/test.php on line 56

I thought this must be possible because phpPgAdmin does it, but I looked at phpPpAdmin sources and found that they connect to a database named template1.

我认为这是可能的,因为phpPgAdmin做了,但是我查看了phpPpAdmin源,发现它们连接到一个名为template1的数据库。

From http://www.postgresql.org/docs/8.1/interactive/manage-ag-templatedbs.html:

从http://www.postgresql.org/docs/8.1/interactive/manage-ag-templatedbs.html:

CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/pgSQL in template1, it will automatically be available in user databases without any extra action being taken when those databases are made.

创建数据库实际上是通过复制现有的数据库来工作的。默认情况下,它复制名为template1的标准系统数据库。因此,该数据库是创建新数据库的“模板”。如果向template1添加对象,这些对象将被复制到随后创建的用户数据库中。这种行为允许对数据库中的标准对象集进行站点本地修改。例如,如果您在template1中安装了过程语言PL/pgSQL,那么它将自动在用户数据库中可用,而不会在创建这些数据库时采取任何额外的操作。

Is there a way to connect without specifying any database?

有没有一种方法可以在不指定任何数据库的情况下进行连接?

3 个解决方案

#1


53  

You have to connect to a database. Which database you could use for a "maintenance database" depends on the installation and the subsequent administration. After a default installation there are 2 databases that could be used for the initial connection - "template1" and "postgres". It's wise to create a new user and a database with the same name and use those.

您必须连接到数据库。您可以将哪个数据库用于“维护数据库”取决于安装和随后的管理。在默认安装之后,有两个数据库可以用于初始连接——“template1”和“postgres”。创建一个同名的新用户和数据库并使用它们是明智的。

#2


9  

Why don't you connect to your Maintenance DB (usually postgres?). I don't know if that'll work. But I believe you'll have to query this database anyway to retrieve the databases available to a given user.

为什么不连接到维护数据库(通常是postgres) ?我不知道这样行不行。但是我相信您将不得不查询这个数据库来检索给定用户可用的数据库。

#3


2  

As the manual of pg_connect says the dbname parameter in the connection string defaults to the value of the user parameter. That's why it uses 'testuser'. If you want dbname to be empty, try "... dbname='' ...".

正如pg_connect手册所述,连接字符串中的dbname参数默认为用户参数的值。这就是为什么它使用'testuser'。如果您希望dbname为空,请尝试“……”dbname =“…”。

#1


53  

You have to connect to a database. Which database you could use for a "maintenance database" depends on the installation and the subsequent administration. After a default installation there are 2 databases that could be used for the initial connection - "template1" and "postgres". It's wise to create a new user and a database with the same name and use those.

您必须连接到数据库。您可以将哪个数据库用于“维护数据库”取决于安装和随后的管理。在默认安装之后,有两个数据库可以用于初始连接——“template1”和“postgres”。创建一个同名的新用户和数据库并使用它们是明智的。

#2


9  

Why don't you connect to your Maintenance DB (usually postgres?). I don't know if that'll work. But I believe you'll have to query this database anyway to retrieve the databases available to a given user.

为什么不连接到维护数据库(通常是postgres) ?我不知道这样行不行。但是我相信您将不得不查询这个数据库来检索给定用户可用的数据库。

#3


2  

As the manual of pg_connect says the dbname parameter in the connection string defaults to the value of the user parameter. That's why it uses 'testuser'. If you want dbname to be empty, try "... dbname='' ...".

正如pg_connect手册所述,连接字符串中的dbname参数默认为用户参数的值。这就是为什么它使用'testuser'。如果您希望dbname为空,请尝试“……”dbname =“…”。