Home:ALL Converter>Flask SQLalchemy can't connect to Google Cloud Postgresql database with Unix socket

Flask SQLalchemy can't connect to Google Cloud Postgresql database with Unix socket

Ask Time:2018-12-14T12:36:25         Author:Isaac

Json Formatter

I am using Flask SQLalchemy in my google app engine standard environment project to try and connect to my GCP Postgresql database.. According to google docs, the url can be created in this format

# postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>

and below is my code

from flask import Flask, request, jsonify
import constants

app = Flask(__name__)

# Database configuration from GCP postgres+pg8000
DB_URL = 'postgres+pg8000://{user}:{pw}@/{db}?unix_socket=/cloudsql/{instance_name}'.format(user=user,pw=password,db=dbname, instance_name=instance_name)

app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # silence the 
deprecation warning
sqldb = SQLAlchemy(app)

This is the error i keep getting:

File "/env/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 412, in connect return self.dbapi.connect(*cargs, **cparams) TypeError: connect() got an unexpected keyword argument 'unix_socket'

Author:Isaac,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53773578/flask-sqlalchemy-cant-connect-to-google-cloud-postgresql-database-with-unix-soc
kurtisvg :

The argument to specify a unix socket varies depending on what driver you use. According to the pg8000 docs, you need to use unix_sock instead of unix_socket.\n\nTo see this in the context of an application, you can take a look at this sample application.",
2018-12-14T17:00:59
Debdut Goswami :

It's been more than 1.5 years and no one has posted the solution yet :)\nAnyway, just use the below URI\npostgres+psycopg2://<db_user>:<db_pass>@<public_ip>/<db_name>?host=/cloudsql/<cloud_sql_instance_name>\n\nAnd yes, don't forget to add your systems public IP address to the authorized network.",
2020-06-24T20:25:34
yy