如何在不指定数据库名称的情况下连接到 PostgreSQL?
我需要连接到一些提供一些凭据的 PostgreSQL 服务器,并为给定用户打印该主机上的可用数据库列表。
我正在尝试:
<?php
$connection = pg_connect("host=localhost user=testuser password=123 connect_timeout=5");
?>
我得到:
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
我认为这一定是可能的,因为 phpPgAdmin 做到了,但我查看了 phpPpAdmin 源代码,发现它们连接到名为 template1
的数据库。
来自 http://www.postgresql.org/docs/8.1/交互式/manage-ag-templatedbs.html:
CREATE DATABASE 实际上是通过复制现有数据库来工作的。经过 默认情况下,它复制名为 template1 的标准系统数据库。因此 该数据库是创建新数据库的“模板”。如果 您将对象添加到 template1,这些对象将被复制到 随后创建了用户数据库。此行为允许站点本地 对数据库中标准对象集的修改。为了 例如,如果您在 template1 中安装过程语言 PL/pgSQL, 它将自动在用户数据库中可用,无需任何额外 建立这些数据库时正在采取的行动。
有没有一种方法可以在不指定任何数据库的情况下进行连接?
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.
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
.
From 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.
Is there a way to connect without specifying any database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须连接到数据库。您可以使用哪个数据库作为“维护数据库”取决于安装和后续管理。默认安装后,有 2 个数据库可用于初始连接 - “template1”和“postgres”。明智的做法是创建一个新用户和一个同名的数据库并使用它们。
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.
为什么不连接到维护数据库(通常是
postgres
?)。我不知道这是否有效。但我相信您无论如何都必须查询该数据库才能检索给定用户可用的数据库。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.正如 pg_connect 手册所说,连接字符串中的 dbname 参数默认为 user 参数的值。这就是它使用“testuser”的原因。如果您希望 dbname 为空,请尝试
"... dbname='' ..."
。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='' ..."
.