简体   繁体   中英

How to connect to PostgreSQL DB with PHP?

As you can already see in the title I want to connect to a PostgreSQL Database I created on a server using PHP and Yii2. Unfortunately I am a total novice when it comes to this kind of job and I have never done it before so I have several questions that I hope some of you can answer.

  1. I am using Yii2 basic and in the config directory there is a db.php file containing the following code:
    return [
        'class' => 'yii\db\Connection',
        'dsn' => 'pgsql:host=localhost;dbname=my1DB',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ];

Is the code actually OK? Instead of localhost I will be using the host IP of course. The db my1DB is actually created and already contains a relation named countries .

  1. I have Yii2 basic installed on my client. Now my question is, do I need to install it on the server as well? It may be a dumb question and I am truly sorry if it is, but I am not sure if I am understanding it all correctly. So, does the db.php file need to be on the server? Or is it OK if it is simply installed on my client?

db.php 需要在运行 PHP 的服务器端,以防 PostgreSQL 服务器运行在 PHP 所在的同一台服务器上,您可以将其保留为 localhost,它应该可以正常工作,只需确保凭据和数据库名称存在。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM