如何在不指定数据库名称的情况下连接到PostgreSQL? [英] How do I connect to PostgreSQL without specifying a database name?

查看:658
本文介绍了如何在不指定数据库名称的情况下连接到PostgreSQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在尝试:

<?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 的数据库。

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.

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


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

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?

推荐答案

必须连接到数据库。可以用于维护数据库的数据库取决于安装和后续管理。默认安装后,有两个数据库可用于初始连接- 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.

这篇关于如何在不指定数据库名称的情况下连接到PostgreSQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆