*(psycopg2.OperationalError) could not connect to server: No such file or directory* GAE Flex enivronment
Hi, I'm getting this error when im trying to connect to my PostgreSQL on my app when its deployed. I followed the instructions here https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres?authuser=0
Both the app and PostgreSQL are in the same project and in the same region.
The error message is
An internal error occurred:
(psycopg2.OperationalError) could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/flask-161002:us-east1:myserver/.s.PGSQL.5432"?
See logs for full stacktrace.
My app.yaml is this
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT app:app
runtime_config:
python_version: 3
env_variables:
# Replace user, password, database, and instance connection name with the values obtained
# when configuring your Cloud SQL instance.
SQLALCHEMY_DATABASE_URI: >-
postgresql+psycopg2://postgres:pass@/postgres?host=/cloudsql/flask-161002:us-east1:myserver
beta_settings:
cloud_sql_instances: flask-161002:us-east1:myserver
In my app.py, I connect wtih
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
The logs from the stacktrace
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: No such file or directory
at connect (/env/lib/python3.5/site-packages/psycopg2/__init__.py:130)
at connect (/env/lib/python3.5/site-packages/sqlalchemy/engine/default.py:385)
at connect (/env/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py:105)
at __connect (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:651)
at __init__ (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:461)
at _create_connection (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:333)
at _do_get (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:1135)
at reraise (/env/lib/python3.5/site-packages/sqlalchemy/util/compat.py:186)
at __exit__ (/env/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py:60)
at _do_get (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:1138)
at checkout (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:516)
at _checkout (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:766)
at connect (/env/lib/python3.5/site-packages/sqlalchemy/pool.py:387)
at _wrap_pool_connect (/env/lib/python3.5/site-packages/sqlalchemy/engine/base.py:2141)
at reraise (/env/lib/python3.5/site-packages/sqlalchemy/util/compat.py:185)
at raise_from_cause (/env/lib/python3.5/site-packages/sqlalchemy/util/compat.py:202)
at _handle_dbapi_exception_noconnection (/env/lib/python3.5/site-packages/sqlalchemy/engine/base.py:1456)
at _wrap_pool_connect (/env/lib/python3.5/site-packages/sqlalchemy/engine/base.py:2145)
at contextual_connect (/env/lib/python3.5/site-packages/sqlalchemy/engine/base.py:2106)
at _connection_for_bind (/env/lib/python3.5/site-packages/sqlalchemy/orm/session.py:403)
at _connection_for_bind (/env/lib/python3.5/site-packages/sqlalchemy/orm/session.py:971)
at connection (/env/lib/python3.5/site-packages/sqlalchemy/orm/session.py:966)
at _connection_from_session (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:2807)
at _get_bind_args (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:2825)
at _execute_and_instances (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:2816)
at __iter__ (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:2795)
at one_or_none (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:2724)
at one (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:2754)
at load_on_ident (/env/lib/python3.5/site-packages/sqlalchemy/orm/loading.py:223)
at _get_impl (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:862)
at get (/env/lib/python3.5/site-packages/sqlalchemy/orm/query.py:829)
at get_single_state (/home/vmagent/app/app.py:65)
at dispatch_request (/env/lib/python3.5/site-packages/flask/app.py:1598)
at full_dispatch_request (/env/lib/python3.5/site-packages/flask/app.py:1612)
at reraise (/env/lib/python3.5/site-packages/flask/_compat.py:33)
at handle_user_exception (/env/lib/python3.5/site-packages/flask/app.py:1517)
at full_dispatch_request (/env/lib/python3.5/site-packages/flask/app.py:1614)
at wsgi_app (/env/lib/python3.5/site-packages/flask/app.py:1982)
@ryanmats any ideas?
@jonparrott @bpn252 i'll take a look and see if i can reproduce the error
@bpn252 does your app work when testing locally?
@ryanmats Hi, yes it does work locally. When I authorize my ip address and connect with
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:pass@IP_POSTGRESQL/postgres"
it works locally.
I made a new project and a new PostgreSQL server and I authorized the ip addresses of my appspot.com, and all works now. I haven't tested the old projected yet.
So I ran through https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres , modifying the sample code at https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/flexible/cloudsql for postgres, and it seemed to work locally and deployed.
Are there any differences between your old project and new project that may have caused the issue?
At a quick glance between the two projects, I don't see any difference besides the IP Address authorization in the Cloud SQL interface. I'm using App Engine for a class project so I can't really test removing the authorized ip addresses in my new project.
Thanks for your help, you can close this issue if you want.
@bpn252 By IP Address authorization are you referring to going to your PostgreSQL instance on the Cloud console and editing the 'Authorized networks' section?
If your App Engine project and PostgreSQL instance are within the same project, I don't think you should have to edit Authorized Networks for your instance.
@jonparrott @ryanmats I experienced same issue. I followed instructions in https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres. Could run it successfully in local, but got error when deployed it.
(psycopg2.OperationalError) could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/panopticon-dev:us-central1:panopticon-data-dev/.s.PGSQL.5432"?
Any idea?
@xiangfeidongsc Hi, I got the error again. However, it seems to have resolved itself when I deployed an hour later. Try deploying now.
@bpn252
SQLALCHEMY_DATABASE_URI: >-
mysql+pymysql://USER:PASSWORD@/DATABASE?unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME
My problem is that when I followed the instructions, I kept the >-. After removing the >- it works 👎 .
Interesting. @bpn252 did you also end up removing the >- ?
@ryanmats Nope, I still have the >- in my app.yaml
@bpn252 hmm interesting. @xiangfeidongsc maybe the problem was just a temporary issue earlier? GCP managed Postgres is still currently in beta.
@ryanmats Yes.. I add it back and it also works... How will this instability last? We want to use it in product.
So we've determined that this is an issue with new App Engine deployments that use CloudSQL (not existing deployments that have already worked) -> we've assigned this bug to someone on the App Engine Infrastructure team to fix.
If you deploy your app and it works, it should continue to work. If one of your new deployments isn't working, you should be able to try again later and it will work.