SSL connection to postgresql Google cloud database


(davidtb) #1

Hi there!

I’m newbie user of Yii and I need to connect my Yii2 application to a Google cloud database (postgres). I need to made the connection using the SSL certificates I already have. I have all the credentials (host, user, 3x ssl, etc).

The thing is I’m able to connect connect to this remote google database using both PgAdmin client and the Google SDK console, but I can’t connect my Yii2 application to this database. I read a lot of documentation and found nothing (Yii, Google, Stackoverflow…etc).

How can I connect to my google cloud database usingYii?

Thanks in advance!


(davidtb) #2

After many tests, I tried this and it seems it (finally) works. I can create models and so from the tables from my public schema in the remote google cloud postgres database. In the database connection in common/config/main-local.php (advanced schema), in the ‘dsn’ parameter:

'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'pgsql:sslmode=verify-ca sslrootcert=path/to/my/server-ca.pem sslcert=path/to/my/client-cert.pem sslkey=path/to/my/client-key.pem hostaddr=(google cloud instance/db host)xx.xxx.xx.xxx user=my-user password=my-password dbname=my-dbname', 'charset' => 'utf8', ],

It’s the same command I use to connect to my database through the Google Shell, but adding the password field, all in one line, not with attributes… the simplest way, never thought this were gonna work…

Hope it will be usefull to somebody with the same problem!! Maybe it also could be added to the documentation.