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

标签 php postgresql pdo

我正在使用以下连接连接到没有数据库的 Postgres SQL,因为我需要稍后获取所有数据库名称以进行配置

try{
    $this->connection = new \PDO($this->database.":host=".$this->host,$this->user,$this->password);
    $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    return $this->connection;
}catch(\PDOException $e){
    echo $e->getMessage();
}

但我收到以下错误

SQLSTATE[08006] [7] FATAL: database "admin" does not exist

以下是在属性中设置的值:

$this->database = pgsql
$this->host = localhost
$this->user = admin
$this->password = admin

谁能帮我解决如何使用 PHP PDO 在不选择任何数据库的情况下连接到 Postgres SQL

最佳答案

有树选项:

  • 邮政系统
  • 模板0
  • 模板1

请注意,连接到数据库需要权限。 Here是解释什么是模板。您可以查找 postgresql 默认数据库。这是非常相似的话题。

关于php - 如何在不在 PDO 中指定数据库名称的情况下连接到 Postgres 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42112781/

相关文章:

javascript - HTML 和 PHP 下拉菜单

ruby - 导轨 4 : activerecord create and update return value?

postgresql - 如何从 PL/pgSQL 中的子选择一次更新多条记录

mysql - 最适合我的应用程序类型的数据库? MySQL? MongoDB? PostgreSQL?沙发数据库?

php - 使用内部连接对表进行分页

php - MySQL 查询语句转换为 Codeigniter Active record 样式查询语句,带有 SUM()

PHP/ curl : delete cookie created with CURLOPT_COOKIEJAR

php - group_concat 问题,需要唯一

javascript - 获取给定值对应的其他值?

php - PDO 准备语句似乎忽略了 HAVING 子句