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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4483139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:09:43  来源:igfitidea点击:

How do I connect to PostgreSQL without specifying a database name?

phppostgresql

提问by Silver Light

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.

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

Is there a way to connect without specifying any database?

有没有办法在不指定任何数据库的情况下进行连接?

回答by Milen A. Radev

You have toconnect 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.

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

回答by ontrack

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?)。我不知道这是否会奏效。但是我相信您无论如何都必须查询此数据库才能检索给定用户可用的数据库。

回答by rik

As the manual of pg_connectsays 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='' ...".