Django heroku connection refused - python

Deployment of my site to heroku seems to be working, until I try to use my login feature then it throws an error:
OperationalError at /login/ 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?
This error seems to occur during template rendering of a form (captcha).
my procfile:
web: waitress-serve --port=$PORT capstonenotespool.wsgi:application
settings.py:
import dj_database_url
DATABASES = { 'default': dj_database_url.config() }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'capstonenotespool',
'USER': 'postgres',
'PASSWORD': '****',
'HOST': 'localhost',
'PORT': '',
}
}
I've tried changing $PORT in the procile to 5432, but this does not work. I've also tried manually setting the PORT in settings to 5432 but this also doesn't work.
I also noted that Django error logs produces these variables:
SERVER_NAME 'localhost'
SERVER_PORT '23994'
SERVER_PROTOCOL 'HTTP/1.1'
SERVER_SOFTWARE 'waitress'
Removing the localhost from HOST in settings gives the following error:
could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Related

Connecting Django to Docker Postgres instance raising django.db.utils.OperationalError

I'm trying to execute a django project on my local machine, the project requires Postgres.
I know close to nothing of docker. I pulled postgres image from docker hub and executed the followin command, as suggested by the instructions in postgres docker hub page.
$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
The docker container is up:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
402180487f68 postgres "docker-entrypoint.s…" 2 hours ago Up 2 hours 5432/tcp some-postgres
But I can't make Django to connect to it. (Django is running on my local machine, not on docker) Django settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'mysecretpassword',
'HOST': 'localhost',
'PORT': 5432,
}
}
If I execute migrations with the settings above the error is:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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 assume the connection is not beign refused, because if I stop the container the error is the same.
Some tutorial/answer suggested HOST should be the container name. To me it doesn't make much sense, as I don't know how Django is supposed to resolve that, but I tried nonetheless:
'HOST': 'some-postgres',
The error raised is:
django.db.utils.OperationalError: could not translate host name "some-postgres" to address: nodename nor servname provided, or not known
I have checked several questions and tutorials, but they all seem to use docker-composer and/or have the django project also inside docker. Still haven't been able to make the project connect to postgres.
I believe you have to forward port 5432 from the docker:
https://docs.docker.com/config/containers/container-networking/#published-ports
Good analogy is a webserver - imagine you would start a django application in a container on port 8000. You couldn't just simply open firefox and navigate to localhost:8000 from within the host as the application is running in an isolated environment.

mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

I am creating a docker file that contains a flask app and mysql. This should run on localhost for now.
The flask app is running and so as the mysql server. I am able to connect to mysql server. The app is not able to connect to db.
Python code connecting
def establish_connection():
config = {
'user': 'root',
'password': 'root',
'host': '127.0.0.1',
'port': '3306',
'database': 'persist'
}
cnx: str = mysql.connector.connect(**config)
print(cnx)
return cnx
Dockerfile
FROM python:3.7.4-buster
WORKDIR /stocksite
ENV FLASK_APP main.py
ENV FLASK_RUN_HOST 0.0.0.0
EXPOSE 5000 32000 3306
COPY . .
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
db:
image: mysql
container_name: db
ports:
- "32000:3306"
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./data/db:/docker-entrypoint-initdb.d
I receive the below error:
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
Docker compose services are available to other services using their name. Your db service can be connected to from your web container using db:3306
config = {
'user': 'root',
'password': 'root',
'host': 'db',
'port': '3306',
'database': 'persist'
}
In the docker-compose you map the port 3306 of the db inside the container to the port 32000 on the host machine.
In the app you should use port 32000 not 3306.
def establish_connection():
config = {
'user': 'root',
'password': 'root',
'host': '127.0.0.1',
'port': '32000',
'database': 'persist'
}
cnx: str = mysql.connector.connect(**config)
print(cnx)
return cnx

django Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

I have made a virtual environment and installed following:
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
and my setting is :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}
and when I do manage.py syncdb it gives the error saying 2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
I dont know whats wrong in here...
I searched for it... Mostly I found if I use virtualenvironment then the above setting is enough... Whats wrong in here ???
Install mysql server
sudo apt-get install mysql-server
then create a database for your purposes:
mysql -u root -p --execute "create database DB_NAME; grant all on DB_NAME.* to DB_USER#localhost identified by 'DB_PASSWORD';"
then you should be fine.
I should point out that the confusion of mysql searching for a socket, even if specifying a port, is because mysql defaults to a socket when specifying localhost as an address, if you want to use tcp/ip then you should put an address like 127.0.0.1 there.
Your HOST should change to '127.0.0.1', you must start service, especially if you are using xamp or lamp.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': '127.0.0.1',
'PORT': '3306',
} #Your HOST should change to '127.0.0.1', you must start service, especially if you are using xamp or lamp

Linking Django and Postgresql with Docker

I have two Docker containers. The first one is Postgresql container, which I run using the following command.
sudo docker run -v /home/mpmsp/project/ezdict/postgresql/data:/var/lib/postgresql/data -p 127.0.0.1:5432:5432 -name my-postgres -d postgres
It is based on official image and it is working perfectly, I can connect to Postgresql from the host.
The second container is a container with my Django application. The image is built using the following Dockerfile (based on this image):
FROM python:3-onbuild
EXPOSE 8000 5432
CMD ["/bin/bash"]
And I run this container with the following command
sudo docker run --link my-postgres:my-postgres -v /home/mpmsp/project/ezdict/ezbkend:/usr/src/app -name my-app -i -t my-app
docker ps output shows that containers are linked
NAMES
my-app/my-postgres, my-postgres
However, when I go to localhost:8000, I see an error page from Django, with the following output
OperationalError at /api-auth/login/
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Request Method: GET
Request URL: http://127.0.0.1:8000/api-auth/login/
Django Version: 1.6.4
Exception Type: OperationalError
Exception Value:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Exception Location: /usr/local/lib/python3.4/site-packages/psycopg2/__init__.py in connect, line 164
Python Executable: /usr/local/bin/python
Python Version: 3.4.1
Python Path:
['/usr/src/app',
'/usr/local/lib/python34.zip',
'/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-linux',
'/usr/local/lib/python3.4/lib-dynload',
'/root/.local/lib/python3.4/site-packages',
'/usr/local/lib/python3.4/site-packages']
Server time: Птн, 10 Окт 2014 12:07:07 +0400
Application's settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
How to make linking work? Thanks in advance
The Dockerfile for your Django image should not expose port 5432 as no Postgresql server will be running in any container created from that image:
FROM python:3-onbuild
EXPOSE 8000
CMD ["/bin/bash"]
Then as you are running the Django container linking it with
--link my-postgres:my-postgres
your settings for the database are incorrect.
In the Django container: 127.0.0.1 refers to the Django container which isn't running any service listening on port 5432.
So your settings.py file should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'my-postgres',
'PORT': '5432',
}
}
As you run your Django container with:
sudo docker run --link my-postgres:db -v /home/mpmsp/project/ezdict/ezbkend:/usr/src/app -name my-app -i -t my-app
then your settings.py file would have to be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'db',
'PORT': '5432',
}
}
Ths syncdb only works AFTER both db and django containers are build and started, then you can manually run the syncdb command with fig/docker-compose/docker.
I am thinking of creating an AT job and let the container run the syncdb itself (and creating an admin user after the syncdb - for creating the necessary tables)

Django 1.5 : OperationalError in windows when running "python manage.py syncdb" using postgresql-psycopg2

This is my settings.py file in my Django project. OS : Windows, hosting : localhost
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'JESS',
'PASSWORD': 'dreamhost',
'HOST': 'localhost',
'PORT': '5432',
}
}
I followed instructions from this blog in installing PostgreSQL and psycopg2
http://shivul.wordpress.com/2010/05/07/installing-django-with-postgresql-on-windows-and-ubuntu/
and when running python manage.py syncdb I get the following error:
OperationalError: Could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432
Could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432
I even tried to edit settings through pgadmin3 application in PostgreSQL installation files, it even throws the same error
Could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432
Could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432
I looked through similar questions in stackoverflow and internet, everything is outdated or not working.
Your server is listening only on IPv4, but localhost is resolving to both an IPv4 and IPv6 address on your host.
Use 127.0.0.1 instead of localhost to force an IPv4 connection, or edit postgresql.conf to tell your PostgreSQL to listen on IPv6 too. Show the listen_addresses line of postgresql.conf for advice on that, along with the output of select version().
I bumped into the exact same message recently but the cause/solution for my problem was different. I am not exactly sure what screwed me up but I think it was when I was seeing what happens when you run the clearsessions function in manage.py.
In the end I needed to restart the postgresql service.
To do this (Windows 7),
launch your task manager (CTRL+ALT+DEL)
go to the services tab
click the services button in the bottom right.
(you can also search for 'View local services' in the start menu)
In the services window:
4. find postgresql-XXXX.
5. Click on it and start it if it is stopped.
If it is already running you might as well restart it since your there and see if it helps. My issue was specifically that the service was not restarting on boot.

Categories

Resources