How to include migrate command while deploying django application in gae - django

I have deployed django application in app engine flexible.
I'm able to run migrations using cloud_sql_proxy. But i want to add migrate step as a part of deployment. Where do i specify that in app.yaml file ?
Also tried
gcloud beta app gen-config --custom
Which creates docker file. on adding migration command in docker file, recieved the following error:
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'password',
'PORT': '5432',
'HOST': 'connection-name',
}}
app.yaml
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT wsgi
beta_settings:
cloud_sql_instances: connection-name
runtime_config:
python_version: 3
Please suggest approach to add migrate command.

You can't add migrate command as part of the deployment process. The app.yaml file just for app engine related configuration. So that what you can do is to connect with your Google Cloud SQL Instance in the local machin and run the migrate command.
The other options is to setup continuous integration. You can visit this link to get the idea about how to setup CI/CD on Travis.

Related

Error with Django application on Cloud Run using Cloud SQL PostgreSQL DB - cannot connect to database

I have this Django project on Google Cloud on Cloud Run. It is supposed to connect to the Cloud SQL PostgreSQL database and work, but it doesn't. Here is the database part of settings.py (I have removed the private data):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': '/cloudsql/project:europe-west4:instance',
'PORT': '5432',
}
}
However, when I try to do anything database-related (for example, log in), it throws the error
OperationalError at /accounts/login/
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/*project*:europe-west4:*instance*/.s.PGSQL.5432"?
If I remove the /cloudsql/ from HOST it gives the error
OperationalError at /accounts/login/
could not translate host name "*project*:europe-west4:*instance*" to address: Name or service not known
When I use the Cloud SQL proxy exe to test on my local computer it works. However, when I deploy it to Cloud Run it doesn't.
Have you enabled connecting to that Cloud SQL instance from Cloud Run?
Try running
gcloud run services update [your cloud run service name] \
--add-cloudsql-instances [project:region:instance]
Also check that your Cloud Run service account has Cloud SQL Client permissions. See this documentation for more information on how to do that.
Try using the full url in host parameter

How to connect to local PostGIS (PostgreS) database using Docker compose in Django project?

I need to connect to existing db in my computer. When I create db service with postgis image, it creates a new database inside docker container. But I need to connect to my local one. Is it possible?
docker-compose.yml :
version: '3'
services:
build: ./
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic --noinput --i rest_framework && gunicorn orion-amr.wsgi:application --bind 0.0.0.0:8000 --workers=2"
ports:
- "${webport}:8000"
env_file:
- ./.env
settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': os.environ.get('pgdatabase'),
'USER': os.environ.get('pguser'),
'PASSWORD': os.environ.get('pgpassword'),
'HOST': os.environ.get('pghostname'),
'PORT': '5432',
}
}
.env file:
pgport=5432
webport=8000
SECRET_KEY=z)mf(y#w7-_8y1)0(v*n#w#lzf)!0=g_rj5$%1w6g-t!7nbk05
pgdatabase=amr_or
pgpassword=root
pghostname=localhost
pguser=postgres
DEBUG=
Now I have this error:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 | could not connect to server: Cannot assign requested address
web_1 | Is the server running on host "localhost" (::1) and accepting
web_1 | TCP/IP connections on port 5432
I really hope you did not expose your real SECRET_KEY to the rest of the internet. Don't forget to change it if you did.
If I remember correctly (based on a question you asked earlier this year), you are running Docker for Windows. Take a look at the docs over here, it says
The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.
Hence, set pghostname to host.docker.internal instead of localhost.
Since you state, that you "need to connect to existing db in (your) computer", I will go ahead and assume you do this for development purposes, so this should suffice.
On production, however, you will need to run the database in a separate container and map the volumes.

difference between localhost and postgres for host in docker

I am developing a django app and trying to run it inside docker. I have an issue that I could not understand so far. while running the app with docker-compose, it seems that the web app cannot connect to the database when i use these configurations:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
but once I change the host to postgres, it works. like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': '',
'HOST': 'postgres',
'PORT': '5432',
}
what is the difference between postgres and localhost. One is running without and issue inside docker and not in development environment in my mac and the other one is the opposite.
# docker-compose.yml
version: '3'
services:
db:
image: postgres
expose:
- "5432"
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Docker Compose actually add the hostnames of all your linked containers to each other.
On you machine, the postgres database is actually running in localhost, that why you have the localhost hostname.
In Compose, it's running in the postgres container, with the hostname postgres, that's why you have the postgres hostname.
If you want, you can create an entry in your host file to redirect postgres to localhost, you will then just have to use postgres everywhere.
Each docker container comes with it's own networking namespace by default. That namespace includes it's own private loopback interface, aka localhost. And they are also attached to networks inside of docker where they have their own internal DNS entry and can talk to other containers on that same network.
When you run your application inside a container with a bridge network, localhost will point to the container, not the docker host you are running on. The hostname to use depends on your scenario:
To talk to other containers, use the container name in DNS.
If it's started by docker-compose, use the service name to talk to one of the containers in that service using DNS round robin.
If it's started inside of swarm mode, you can use the service name there to go to a VIP that round robin load balances to all containers providing that service.
And if you need to talk to the docker host itself, use a non-loopback IP address of the docker host.

Docker messed with local django/postgres projects. Can't run project on localhost

I stocked at postgresql/django issue. After attempt of running the localhost server got the following error
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host "172.17.0.1", database "closerdb", SSL off
According to many similar issues and answers last lines at pg_hba.conf look this way:
host all all 0.0.0.0/0 md5
local replication postgres trust
host replication postgres 127.0.0.1/32 trust
host replication postgres 172.17.0.1 trust
host replication postgres ::1/128 trust
I have restarted postgres after editing .conf file, but error remains. I supposed that it was cause by docker installation, because normally django project has to run on loclahost(127.0.0.1) and 172.17.0.1 is a default docker ip.
How to fix all that and avoid such problems in future?
EDIT: django database settings. worked fine previously before installing docker:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'closerdb',
# 'HOST': '/tmp/mysql.sock',
# 'PORT': '8000',
# 'HOSTNAME': '',
'USER': 'thekotik',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}
I assume your django just wants to connect to a database called closerdb. In this case the corresponding pg_hba.conf entry should look like that:
host closerdb postgres 172.17.0.1 trust
From the postgresql docs:
The value replication specifies that the record matches if a replication connection is requested (note that replication connections do not specify any particular database). Otherwise, this is the name of a specific PostgreSQL database
I also want to point out that you should use trust carefully depending on your environment, because:
Allow the connection unconditionally. This method allows anyone that can connect to the PostgreSQL database server to login as any PostgreSQL user they wish, without the need for a password or any other authentication.

while deplyoing django app on heroku postgres doesn't work

I am trying to deploy my django app on heroku server,i followed the instructions from this website https://devcenter.heroku.com/articles/getting-started-with-python#introduction .it worked fine till , "heroku open" command.When i came to the part where i need to host my database using " heroku run python manage.py syncdb" command , it failed showing the mesage "OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?". I tried lots of fixes including the one suggested here Deploying Django app's local postgres database to heroku? and http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html .I tried all the solutions including editing the "listen_address" = '*' and tcpip_socket='true' in postgresql.conf and editing the ipv4 and v6 values in pg_hba.conf to
host all all 127.0.0.1 255.255.0.1 trust
host all all 10.0.0.99/32 md5
host all all 0.0.0.0/0 .
But none of them worked .I am guessing the problem arises because heroku can not connect to my local postgres server.This is strange because i'm able to access the postgres server via pgadmin.
And also in the django settings.py looks like this
DATABASES =
{
'default':
{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_test',
'USER': 'postgres',
'PASSWORD': '******',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '5432',
}
}
Do i need to change this and use heroku's database settings instead??
localhost on the server points to the server not your local machine. The reason why is because the server running your django code will try and resolve the dns name localhost and it has a pointer to 127.0.0.1 which is local to the server resolving that name. That will NOT point to your computer you are working on.
You need to get an instance of postgres on heroku and change HOST: 'xxx.xxx.xxx.xxx to the IP address of your new postgres instance in your django settings.