I would love if anyone can please help me with the following:
Summary
I'm on a newly created GCP project and I'm trying to connect from App Engine
(a python app) to Cloud SQL
(a postgresql instance). But I'm getting an error:
OperationalError: (psycopg2.OperationalError) connection to server on socket "/cloudsql/<project_id>:southamerica-east1:test-instance/.s.PGSQL.5432" failed: Connection refused
The python app / App Engine (Standard environment)
The app is an API created with FastAPI
. It uses sqlmodel/sqlalchemy
for handling the database and psycopg2-binary
as adapter. This is the code that connects to the database and create the tables:
import sqlalchemy
from sqlmodel import create_engine
connection_name = "<project_id>:southamerica-east1:test-instance" # Here I omitted the real project id
url = sqlalchemy.engine.url.URL.create(
drivername="postgresql psycopg2",
username="some-username",
password="some-password",
database="some-database-name",
connection_name = connection_name,
query={"host": "{}/{}".format("/cloudsql", connection_name)},
)
engine = create_engine(url)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
The strange thing is that, I've successfully deployed the same app in the past to App Engine
(a few months ago, in another project and an older version of the app) and it was connecting to Cloud SQL
. And according to me, I've used the same code for the connection, in both cases.
Expanded Error
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server:
Connection refused
Is the server running locally and accepting
connections on Unix domain socket
"/cloudsql/<project_id>:southamerica-east1:test-instance/.s.PGSQL.5432"?
Background on this error at: https://sqlalche.me/e/14/e3q8)
Notes / Things that I have tried
- I'm able to connect to the
Cloud SQL
database from my localpgAdmin 4
. - If I remove the database part of the app, the app gets deployed to
App Engine
correctly. - I've tried using
psycopg2
instead ofpsycopg2-binary
. - I've tried restarting/shutting down the cloud sql instance.
- I've tried using a different adapter, an async one (I don't this is the way though, because as I mentioned before I was able to deployed before using
psycopg2
)
CodePudding user response:
Summary
The problem was that I had the Cloud SQL Admin API disabled. I activated that an it worked.
Details
I was only looking at the logs through my local console, using the command gcloud app logs tail -s default
and the only error I was able to see with that, was the one I posted in the original question.
But, following @kurtisvg advices, I checked App Engine logs directly through the GCP console and there, an other error appeared. The error was Error 403: Cloud SQL Admin API has not been used in project before or it is disabled.
. I followed the instructions and it worked.
@kurtisvg thanks again!