django cloud sql sample for postgresql
Please add sample for django postgresql settings.
I tried the instructions at: https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres
But they are not working. I am getting unix socket error. More details: http://stackoverflow.com/questions/42950418/postgres-settings-for-django-on-google-app-engine-flexi
/cc @waprin
I am also experiencing a similar issue. See here: http://stackoverflow.com/questions/42836927/error-connecting-django-to-google-cloudsql-postgres-database-in-google-app-engin
@KushGoyal @agmathew thanks for the feedback. i'll take a look at this today.
@KushGoyal @agmathew So I was able to get it to work when deployed, and wasn't able to reproduce your error message :/
I would recommend doing the Django on App Engine Flexible tutorial (https://cloud.google.com/python/django/flexible-environment) from the beginning step-by-step with these PostgreSQL specific changes:
-
When you get to 'Create a Cloud SQL Instance' step 1, use https://cloud.google.com/sql/docs/postgres/create-instance and make sure you create a PostgreSQL instance instead of a Cloud SQL instance. When you create the instance, you'll be asked to create a password. This password is for the 'postgres' default user for this PostgreSQL instance.
-
Skip step 2 of 'Create a Cloud SQL Instance,' since you set the password when creating the instance.
-
In 'Initialize your Cloud SQL instance' step 1, use this command to use port 5432 instead of 3306: "./cloud_sql_proxy -instances="[YOUR_INSTANCE_CONNECTION_NAME]"=tcp:5432"
-
In 'Configure the Database Settings,' when you are editing mysite/settings.py, also change 'ENGINE': 'django.db.backends.mysql' to 'ENGINE':'django.db.backends.postgresql'
-
While you are configuring database settings here, also edit your requirements.txt file and add psycopg2==2.7.1
If you do this and still have difficulties, let us know along with more details about where you got stuck and what error messages you got!
@ryanmats it worked! Although I didn't change a thing 😲 Thanks for the help
@ryanmats Same for me, it's working now, but I haven't touched anything. Do you know if any changes were made to GAE or CloudSQL Postgres in the last few days? Is there somewhere where we can track changes like this? We're currently evaluating whether to use GAE for a new product and I'm a little concerned about a service "fixing" itself.
@KushGoyal @agmathew Great to hear that it works! I just filed a bug for the Cloud SQL / Postgres team to ask them about this - will let you know what they say.
@jonparrott do you know if there are any resources for customers to track product changes? (see @agmathew 's comment above)
@agmathew @KushGoyal Postgres for Cloud SQL is in beta, so it's expected that things will be changing rapidly as we approach GA, especially things around reliability. I highly recommend joining the user group so that you can provide us with feedback on the beta.
For Cloud SQL, we have release notes published here: https://cloud.google.com/sql/docs/release-notes
For GAE flexible, we have release notes published here: https://cloud.google.com/appengine/docs/flexible/python/release-notes
(Closing as this appears to be resolved, but feel free to comment if you have any more questions/issues).
The issue is repeating again on a new project. I have followed the exact steps which are:
-
create a postgresql instance
-
create a db
-
get name of connection using command:
gcloud sql instances describe <instance-name> -
add connection name to app.yaml
beta_settings:
cloud_sql_instances: <connection-name>
- use host as
/cloudsql/<connection-name>
I tried to deploy the project again and this time it worked without changing anything.
I don't know what changed. Perhaps this issue is related to app engine not detecting the presence of postgresql db.
@KushGoyal we'll ping the CloudSQL engineering team about this. thanks for the feedback.
@KushGoyal :
- What was the error message that you received?
- What was the format of the SQLALCHEMY_DATABASE_URI that you used (eg: postgresql+psycopg2://username:password@/databasename?host=connection-name)
- Did you try connecting to the instance from your local machine by using cloud-sql-proxy? (look at the Setting up your local environment instructions here : https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres)
@ankushagarwal
- error message:
could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/cloudsql/instanse-connection-name/.s.PGSQL.5432"?
- I am using django.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USERNAME'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': '/cloudsql/<connection-name>'
},
}
- I connected using static ip.
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.
@ryanmats thanks for the update. Is it possible to know when will this be fixed?
@ryanmats Is there any pattern to this error? I have deployed my project 5-6 times now but the error is still there.
@KushGoyal It looked like the problem had gone away for a while but I'll take a look again this afternoon. Make sure to double-check that you have the right app.yaml and settings.py settings based on your project.
You are using Django with Postgres on App Engine Flex, correct?
@KushGoyal I just tested a deployment of a Django App Engine Flex App with Postgres and it works. Can you please go through this tutorial https://cloud.google.com/python/django/flexible-environment (which now has complete Postgres instructions), making sure you set the correct app.yaml / settings.py settings and try deploying again? (One time recently I deployed with the wrong instance connection string and this messed things up)
The problem should be resolved but this weekend I will continually re-deploy this app to see if the problem arises again at any point.
@ryanmats Yes, I am using django with postgres on app engine flex.
I have checked my settings and they are exactly as described in the docs. I am using the exact same code in my other project and it is working. Even in that project it didn't work the first time. But after redeploying it started working. But this new project is not working after redeploying several times.
I am posting my db settings and app yaml below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USERNAME'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST')
},
}
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT myproject.wsgi
runtime_config:
python_version: 3
env_variables:
DB_NAME: 'djangodb'
DB_USERNAME: 'postgres'
DB_PASSWORD: 'mypassword'
DB_HOST: '/cloudsql/project-id:us-central1:db-instance-name'
beta_settings:
cloud_sql_instances: project-id:us-central1:db-instance-name'
the error:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/<connection-name>/.s.PGSQL.5432"?
the connection name is exactly matching from the command:
gcloud beta sql instances describe <db-instance-name>