Reputation: 366
I used to simply use Python to connect to my Postgresql from a Mac with :
username = 'xx' # DB username
password = 'xx' # DB password
host = 'ip' # Public IP address for your instance
port = '5..'
database = 'test'
engine = sqlalchemy.create_engine('postgresql://' + username + ':' + password + '@' + host + ':' + port + '/' + database)
Which works great.
However this will not work on a Cloud Function
and we need the Proxy Connection
.
I read the docs multiple times, and they are messy and unfocused, transferring you from one link to another.
EDIT TLDR;
Can I simply connect from a Function without a proxy? the amount of things needs to be done and understood to do that consume a lot of time and I simply want my Function
to connect to DB like I showed here, without terminal installations and others.
I need help for dumb people that don't understand the if they need to "start the Cloud SQL Auth proxy using TCP sockets, Unix sockets, etc" and just want to connect and build.
I added roles on IAM
under App Engine to allow SQL Admin
.
I downloaded the proxy using terminal which created a file in the project:
curl -o cloud_sql_proxy
https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.386
I then need to do something like :
./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306
Which I am not sure I understand, I know my connection name
, but what is the ./
? where exactly I run this from ?
I changed the code to :
path="/cloudsql/test-33xxxx:us-central1:user/.s.PGSQL.5432"
engineSKT = sqlalchemy.create_engine('postgresql://' + username + ':' + password + '@/' + database + "?host=" + path)
What else do I need to install using the terminal? the docs are not clear, there are many options (TCP/sock/socket) I am not familiar with those.
Do I need to also install the same things on my Cloud Function
as well ? not clear.
How exactly do I change this code to work ? I understand that I need something like :socketPath: '/cloudsql/INSTANCE_CONNECTION_NAME'
I do not understand why if under the same project we have a Function
and an SQL
, Google can't automatically let them connect ? they are obviously were created by the same person and safe to talk.
Upvotes: 0
Views: 892
Reputation: 366
To anyone who struggle, I just can't understand why but Google makes it sound so complicated where in fact you do not have to do anything but change your code.
For beginners, like me, they might think they need "proxy" and many complicated installations, they don't.
This :
path="/cloudsql/test-33xxxx:us-central1:user"
engineSKT = sqlalchemy.create_engine('postgresql+psycopg2://' + username + ':' + password + '@/' + database + "?host=" + path)
is the path to connect from your Function
.
and this :
host = 'your_sql_ip'
engine = sqlalchemy.create_engine('postgresql://' + username + ':' + password + '@' + host + ':' + port + '/' + database)
on development. The later require you to allow this IP on the console.
There are other ways to make it work which are "recommended" (proxy) but this word does not include your dev time, and you may spend lots of it to understand this proxy concept.
Upvotes: 1