python-docs-samples icon indicating copy to clipboard operation
python-docs-samples copied to clipboard

django cloud sql sample for postgresql

Open staticmetal opened this issue 9 years ago • 43 comments

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

staticmetal avatar Mar 22 '17 12:03 staticmetal

/cc @waprin

theacodes avatar Mar 22 '17 16:03 theacodes

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

agmathew avatar Mar 23 '17 18:03 agmathew

@KushGoyal @agmathew thanks for the feedback. i'll take a look at this today.

ryanmats avatar Mar 23 '17 19:03 ryanmats

@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:

  1. 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.

  2. Skip step 2 of 'Create a Cloud SQL Instance,' since you set the password when creating the instance.

  3. 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"

  4. 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'

  5. 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 avatar Mar 24 '17 00:03 ryanmats

@ryanmats it worked! Although I didn't change a thing 😲 Thanks for the help

staticmetal avatar Mar 24 '17 04:03 staticmetal

@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.

agmathew avatar Mar 24 '17 04:03 agmathew

@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)

ryanmats avatar Mar 24 '17 06:03 ryanmats

@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

theacodes avatar Mar 24 '17 17:03 theacodes

(Closing as this appears to be resolved, but feel free to comment if you have any more questions/issues).

theacodes avatar Mar 24 '17 17:03 theacodes

The issue is repeating again on a new project. I have followed the exact steps which are:

  1. create a postgresql instance

  2. create a db

  3. get name of connection using command: gcloud sql instances describe <instance-name>

  4. add connection name to app.yaml

beta_settings:
  cloud_sql_instances: <connection-name>
  1. use host as /cloudsql/<connection-name>

staticmetal avatar Apr 19 '17 09:04 staticmetal

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.

staticmetal avatar Apr 19 '17 11:04 staticmetal

@KushGoyal we'll ping the CloudSQL engineering team about this. thanks for the feedback.

ryanmats avatar Apr 19 '17 19:04 ryanmats

@KushGoyal :

  1. What was the error message that you received?
  2. What was the format of the SQLALCHEMY_DATABASE_URI that you used (eg: postgresql+psycopg2://username:password@/databasename?host=connection-name)
  3. 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 avatar Apr 19 '17 22:04 ankushagarwal

@ankushagarwal

  1. 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"?

  1. 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>'
    },
}
  1. I connected using static ip.

staticmetal avatar Apr 20 '17 05:04 staticmetal

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 avatar Apr 24 '17 21:04 ryanmats

@ryanmats thanks for the update. Is it possible to know when will this be fixed?

staticmetal avatar Apr 27 '17 03:04 staticmetal

@ryanmats Is there any pattern to this error? I have deployed my project 5-6 times now but the error is still there.

staticmetal avatar Apr 28 '17 10:04 staticmetal

@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?

ryanmats avatar Apr 28 '17 19:04 ryanmats

@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 avatar Apr 29 '17 04:04 ryanmats

@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>

staticmetal avatar Apr 29 '17 05:04 staticmetal