while deplyoing django app on heroku postgres doesn't work - django

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.

Related

What do I do to fix my PostgreSQL database connection problem in Django?

I am trying to connect my Django project to a PostgreSQL database I created on AWS but I keep getting this error message each time I run py manage.py runserver.
Here is the error message:
django.db.utils.OperationalError: connection to server at "database-1.ce7oz69tjzjc.us-east-1.rds.amazonaws.com" (52.3.150.111), port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
I followed the tutorial on w3school and followed all instructions. I created a PostgreSQL database then I updated my settings with the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'codekeen',
'USER': 'baseuser',
'PASSWORD': 'database',
'HOST': 'database-1.ce7oz69tjzjc.us-east-1.rds.amazonaws.com',
'PORT': '5432'
}
}
as I was told to, but I keep getting the error each time try migrating or running the server.
What do I do to fix this?
First of all, make sure that postgresql is up and running. You can check with systemctl status postgresql-xx where xx is your postgresql version. After that,You need to allow TCP/IP connections to be able to connect postgresql database. Open the postgresql.conf file
vim /etc/postgresql/8.2/main/postgresql.conf
or
vim /var/lib/pgsql/data/postgresql.conf
And set the listen addresses to "*" or IP addresses.
listen_addresses='*'
or
listen_addresses='xxx.xx.x.x'

Django can not connect to PostgreSQL DB

When I`m trying to connect Django Server to PostgreSQL db there is an error:
" port 5433 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? "
I`m using Windows 10, Pycharm, Debian
Settings in Django:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'ps_store_db',
'USER': 'zesshi',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5433',
}
}
Tried to check connection with DBeaver and all`s good there, but still cant connect with Django
My firewall is off, i was trying to change from 5432 to 5433
Dbeaver connection
Dbeaver connection 2
Try restarting/reinstalling postgres. Most likely DBeaver has blocked the port that's why you are not able to connect from django.
(Sorry for posting answer, i am unable to comment yet)
The default port of the PG database is 5432. If you need to change this port, you need to edit the postgresql.conf file and restart the database service before the client can access it.
You also need to check the pg_hba.conf file. The recommended configuration is as follows:
host all all 0.0.0.0/0 md5

How to include migrate command while deploying django application in gae

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.

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.