简体   繁体   中英

How to connect PostgreSQL database to Google Cloud Compute Engine?

I am trying to connect my Cloud SQL PostgreSQL databse to my Compute Engine instance, via psycopg2. My connection string as follows:

con = psycopg2.connect(
                dbname=config('DATABASE'),
                user=config('USER'),
                password=config('PASSWORD'),
                host=config('HOST'),
                port=config('PORT')
            )

using port 5432 and the public IP address of the postgresql database. database, user, and password are all correct. When I run this code via the compute instance i get the following error

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    bot.add_cog(Profile(bot))
  File "/home/username/appname/Profile.py", line 22, in __init__
    port=config('PORT')
  File "/home/username/.local/lib/python3.7/site- 
packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  password authentication failed for user 
"mygcloudusername"
FATAL:  password authentication failed for user "mygcloudusername"

For some reason it seems to be trying to connect via my gcloud username as the user, even though the correct user is specified in the connection string. Where have I gone wrong here? How can i connect this correctly?

Could you verify if CloudSQL API is enabled [1] ? As information, here is the documentation [2] to create a PostgreSQL instance with the recommended configuration and setting.

I also recommend the documentation[3] about 'Connecting to CloudSQL from external applications'. Here's a sample of code written in python [4] .

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