0

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?

1
  • Is Postgres deployed on Cloud SQL? If not, where is hosted? Commented Sep 4, 2019 at 13:24

1 Answer 1

0

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].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.