0

This is my first Django deployment, on Google App Engine flexible.

After deployment, all static content is rendered correctly, but as soon as the request hits an ORM code, it fails (server error 503). I'm using PostgreSQL and I think I link it correctly on my settings.py and app.yaml
DB_HOST: '/cloudsql/my-app:us-central1:my-app-postgresql'.
DB_NAME: 'my-db' ...

Do I need to know anything special about the deployment of Django PostgreSQL to App Engine? During the deployment, all tables and data will be recreated, right?

Finally, I deploy with DEBUG = True and see ProgrammingError at / the "my-app_my-app" relationship does not exist.

How do I export all my database to Google Cloud SQL database?

Based on this, it's look like i will not able to use ORM. Do i have to use "Django Nonrel" ?

Zorro
  • 1,085
  • 12
  • 19

2 Answers2

2

While searching it seems that the transfer of the database is opposed to the stage of the proxy connection thanks to the file cloud_sql_proxy.exe. but I deleted the DB and resumed this step without success.

Finally my solution consists of 2 Steps:

  1. After deployment, connect in SSH to the :
    From Google console > App Engine > Instances > SSH > Display the gcloud command (faster) and copy the command.
    • On your gcould SDK :
    Connect to your instance with the copied command then:
    sudo docker ps and copy your app CONTAINER ID
    sudo docker exec -it <CONTAINER ID> /bin/bash
    python3 manage.py makemigrations
    python3 manage.py migrate
    python3 manage.py createsuperuser

This will create all the necessary tables. If you don't have any data in your local postgreSql, everything is done, otherwise step 2.

  1. Dump your database and import it to Cloud SQL (doc)
    • From your local dump your postgre database with :
    pg_dump -h localhost -U postgres my_database_name | gzip > backup.gz
    • From your bucket upload the file backup.gz
    • From the instance of Postgres on the SQL Cloud, import your dump from your bucket.

Don't forget to delete it from the bucket in case it is on a public folder.

Zorro
  • 1,085
  • 12
  • 19
  • Would be great if we can add thiose migrations command on the app.yaml file to be run after deploy, but i do not know how to do it. – Zorro Aug 02 '20 at 19:59
  • I have created a [Feature Request](https://issuetracker.google.com/162723491) on your behalf regarding this functionality. You may "star" it so that it receives visibility and also add your email in the "CC" section so that you may receive further updates on this thread. – Artemis Georgakopoulou Aug 03 '20 at 12:15
  • Good idea! Thanks. – Zorro Aug 03 '20 at 23:24
0

I just had the same issue as you today, and I solved it.

If you'r like me, kinda new to all cloud stuff there is one rule to remember, by default everything is locked, blacklisted. That's means that by default your App Engine service doesn't have the right to connect to your DB. Thats why you have a 503 because the request is rejected.

2 ways to solve it. By using the private IP address of you DB or the public IP. I choose the private IP because the request stay in my VPC, so more secure.

Here the GCP's documentation

But to be quick :

  1. I have create a VPC Connector in the same region as my project.

  2. I have add the VPC connector to my app.yml file

    vpc_access_connector: name: projects/my-project-12345/locations/europe-west1/connectors/my-connector

And it's working like a charm !

PS : Also, if you are like me and did 1000000 tests, don't forget to delete all unused version of your app because I read that cost $.

Ragnar
  • 2,550
  • 6
  • 36
  • 70
  • I tried with the private IP of my instance, it didn't work, but your first solution is the DB IP! Interesting. The second solution also seems to be an interesting approach! To be kept in mind for a similar problem. But I finally managed to figure it out on my own and I think my solution is simpler. Thank you! +1 – Zorro Aug 02 '20 at 19:56