Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 357
Isaac Avatar asked Dec 14 '18 04:12

Isaac


1 Answers

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.

To see this in the context of an application, you can take a look at this sample application.

like image 53
kurtisvg Avatar answered Oct 13 '22 22:10

kurtisvg