I would love if anyone can please help me with the following:
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 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.
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)
Cloud SQL
database from my local pgAdmin 4
.App Engine
correctly.psycopg2
instead of psycopg2-binary
.psycopg2
)The problem was that I had the Cloud SQL Admin API disabled. I activated that an it worked.
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!