Yii2无法连接到postgresql
问题描述:
我在使用yii2和postgresql数据库时遇到此错误。Yii2无法连接到postgresql
SQLSTATE[HY000] [2002] No such file or directory
Caused by: PDOException
SQLSTATE[HY000] [2002] No such file or directory
我配置文件主local.php这样的:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
顺便说一句,当我用mysql它的工作。
答
这显然是一个配置问题。
- 打电话yii2提供并检查是否安装了PDO PostgreSQL的扩展requirements page。
- 让你的配置正确。 PostgreSQL与MySQL不同。每个数据库群集至少包含一个名为的数据库。数据库至少包含一个名为的模式,该模式又包含表格。
有了这些知识,你的主local.php应该是这样的:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=YOURDATABASE',
'username' => 'YOURPOSTGRESUSERNAME',
'password' => 'YOURPOSTGRESPASSWORD',
'charset' => 'utf8',
'schemaMap' => [
'pgsql' => [
'class' => 'yii\db\pgsql\Schema',
'defaultSchema' => 'public' //specify your schema here, public is the default schema
]
], // PostgreSQL
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
检查HTTP的'输出://本地主机/ requirements.php'([文件](HTTP:/ /www.yiiframework.com/doc-2.0/guide-start-installation.html#verifying-installation))并查看是否在服务器中安装了** PDO PostgreSQL扩展**。 –
没关系(通过) – user5930682
也尝试'localhost'而不是'127.0.0.1' –