Changed connection driver for ease of use.#2194
Changed connection driver for ease of use.#2194kurtisvg merged 11 commits intoGoogleCloudPlatform:masterfrom
Conversation
The current code does not work if tried to implement as is. There are bugs as described in the first comment of this StackOverflow question. https://stackoverflow.com/a/53784015/5056741
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
|
I signed it! |
|
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
gguuss
left a comment
There was a problem hiding this comment.
Looks like you need to push a commit removing the trailing whitespace for your PR to pass lint.
nox > flake8 --show-source --builtin gettext --max-complexity 20 --import-order-style google --exclude .nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E226,E24,E704,W503,W504,I100,I201,I202 --application-import-names main,templates .
./main.py:45:25: W291 trailing whitespace
database=db_name
^
nox > Command flake8 --show-source --builtin gettext --max-complexity 20 --import-order-style google --exclude .nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E226,E24,E704,W503,W504,I100,I201,I202 --application-import-names main,templates . failed with exit code 1
nox > Session lint(sample='./cloud-sql/postgres/sqlalchemy') failed.
The command "./scripts/travis.sh" exited with 1.
gguuss
left a comment
There was a problem hiding this comment.
I'm less familiar with the psycop connector, will have to re-review
|
@kurtisvg Can you comment on whether we should be using pg8000 or psycopg2? |
| Flask==1.0.2 | ||
| SQLAlchemy==1.2.17 | ||
| pg8000==1.13.1 | ||
| psycopg2==2.8.2 |
There was a problem hiding this comment.
Update versions here.
Flask==1.1.1
SQLAlchemy==1.3.6
pg8000==1.13.2
| db = sqlalchemy.create_engine( | ||
| # Equivalent URL: | ||
| # postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432 | ||
| # postgres+psycopg2://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432 |
There was a problem hiding this comment.
Are you sure the parameter is unix_socket? I believe I've seen it as unix_sock in some adapters.
There was a problem hiding this comment.
I'd prefer to keep the pg8000 adapter and just show the correct parameter. If you are using the psycopg2 connector, it seems that you will need to install additional native dependencies for your platform.
There was a problem hiding this comment.
It's cool if it works with the pg8000 adapter. I only changed it because the pg8000 driver wasn't working as per demo and was raising eroors as mentioned at the top. I tested it with psycopg2 instead and it worked. Because the PR is over a month old, I can't find the exact piece of working code that I tested with but I double checked again and this piece works:
db = sqlalchemy.create_engine(
# Equivalent URL:
'postgres+psycopg2://{}:{}@/{}?host=/cloudsql/{}'.format(db_user, db_pass, db_name, cloud_sql_instance_name)
)
Also StackOverflow says that 'unix_sock' should be 'host'.
There was a problem hiding this comment.
I'm testing now, thank you btw for taking the time to make the PR :)
There was a problem hiding this comment.
I think you are right, just will confirm.
There was a problem hiding this comment.
The parameters get passed directly to the driver, and thus change depend on which driver you are using. pg8000 requires unix_sock: http://pybrary.net/pg8000/dbapi.html
I tried using pg8000 as is initially. It returned 'raise InterfaceError("communication error", e)'. Changing it to psycopg2 worked. That's when I opened the pull request. |
|
We use pg8000 specifically because it's a pure python driver and thus it works in many situations where psycopg2 doesn't. The issue you are experiencing is because pg8000 requires the user to specify the suffix The suffix was added to the sample in #2235. It looks like the comment is still incorrect however. |
|
I've changed both main.py and requirements.txt to use pg8000 by default. I've also changed main.py to the correct query statements. |
|
@pranaynanda Thanks again for the PR, I'm thinking it may make sense to leave as pg8000 to simplify getting the requirements installed. Anecdotally, the sample as configured in Master is working for me and I'm unable to get psycopg2 installed on my dev machines as the sources for postgresql are missing. What's weird though is that I'm unable to install the dependency pg8000==1.13.2, only pg8000==1.12.5, potentially because I'm testing with Python2. When I first ran the sample, I had the wrong DB name which caused the same exception you were seeing. |
|
Sure. Makes sense. The code might have changed against another PR after I created this PR. The one in master branch works now. I had an older copy that did not have the |
kurtisvg
left a comment
There was a problem hiding this comment.
Thanks for taking the time to PR this. Apologies it sat unattended for so long.

The current code does not work if tried to implement as is. There are bugs as described in the first comment of this StackOverflow question. https://stackoverflow.com/a/53784015/5056741. To be precise, it raises 'raise InterfaceError("communication error", e)' error which appears to be related to a bug in
postgres+pg8000driver