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

编程入门 行业动态 更新时间:2024-10-25 14:21:57
本文介绍了如何在不指定数据库名称的情况下连接到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.

来自 www.postgresql/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?

本文发布于:2023-10-24 17:24:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   情况下   名称   数据库   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!