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

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.

Related

"Unable to connect: Adaptive Server is unavailable or does not exist" in SQL Server Docker Image

I am trying to connect to a Docker container running SQL Server using pymssql and I am getting the following error,
Can't connect to db: (20009, b'DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist (localhost)
Net-Lib error during Connection refused (111)
DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist (localhost)
Net-Lib error during Connection refused (111)\n')
Now, I know similar questions have been asked before, such as the one given below,
Database connection failed for local MSSQL server with pymssql
However, most of the solutions given revolve around opening up the Sql Server Configuration Manager and making changes through the UI or allowing network traffic to pass through. However, I am not entirely certain how to do this with a Docker container.
How can I resolve this? Is there a different SQL Server I should run? Or a different Python package I should use to establish the connection?
I have spin up my container based on the instructions given here,
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver16&pivots=cs1-bash
Given below are the parameters I am using to establish the connection,
{
"host": "localhost",
"port": "1433",
"user": "SA",
"password": "<my-password>",
"database": "TestDB"
}
Update: I am actually trying to connect to the SQL Server Docker instance from within another container. It is the MindsDB container. This MindsDB container is, however, using pymysql to establish the connection.
Update: Given below are the two commands I am using to run my containers.
SQL Server:
sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<my-password>" \
-p 1433:1433 --name sql1 --hostname sql1 \
-d \
mcr.microsoft.com/mssql/server:2022-latest
MindsDB:
docker run -p 47334:47334 -p 47335:47335 mindsdb/mindsdb

Access on local PostgreSQL Database from the inside of a docker container with a flask-webapp using a psycopg2 connection

I am relatively new to this topic. I try to build a webapp using flask. The webapp uses data from a postgresql database which is running local (Mac OS Monterey 12.2.1).
My application uses a python code which accesses data from the database by connecting to the database with psycopg2:
con = psycopg2.connect(
host = "192.168.178.43"
database = self.database,
port = "5432",
user = "user",
password = "password")
I already added the relevant entries to the "pg_hba.conf" file and to the "postgresql.conf" file to the needed configurations for an access in my home network. But i still got an error when starting the container. The app runs perfect outside the container. I think I miss some important steps to complete a successful connection.
This error is the following
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 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?

Cannot connect to postgresql through sqlalchemy from different computer

I think do not understand properly how does sqlalchemy works, I have tried to connect to postgresql running on some cloud server from my local computer:
db = create_engine('postgresql://ubuntu#172.23.160.212:5432/dbname')
but that causes the error:
Is the server running on host "172.23.160.212" and accepting
TCP/IP connections on port 5432?
I have checked the port and host also exists.
I thought I should connect to the host using ssh first:
with SSHTunnelForwarder((172.23.160.212, 22), ssh_username='ubuntu', remote_bind_address=(127.0.0.1, 3306)) as server:
db = create_engine('postgresql://postgres#127.0.0.1:5432/dbname')
But that did not help.
I have solved the problem partially,
If one opens ssh connection in bash (ssh ubuntu#172.23.160.212 -L 5432:localhost:5432 -N -n -f), then one can open db through python:
db = create_engine('postgresql://tissuemaps#localhost:5432/dbname')
If I understand correctly, the connection to postgres directly should also have worked, and why it does not, I do not know.
I think the problem is TCP connection is not enabled and have to modify your pg_hba.conf file to allow the connection. Add lines in the config file to allow connection
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Apart from that you can also check postgresql.conf (/etc/postgresql/9.3/main/postgresql.conf) to check other postgres configs are what you expect like port number etc. Also add below line in config file to accept all the connections
listen_addresses = '*'
You need to restart the postgres service for the changes to be picked up
sudo service postgresql restart
https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html
https://www.postgresql.org/docs/9.1/static/runtime-config-connection.html

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.

Postgresql socket error when running Django's syncdb (mac OS 10.7.5)

I'm trying to run Django's syncdb operation and am getting the following error:
psycopg2.OperationalError: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I've read the answers to this question and have tried the following:
changing "listen_address" to "localhost" or "*" in /Library/PostgreSQL/9.1/data/potsgresql.conf
changing "host" to "localhost" in my settings.py file (for Django)
user is set to "postgres" with correct password in settings.py
(I restarted the postgresql server after each step.)
I've also checked my pg_hba.conf file to see if the Unix domain socket was accepting connections from all users (based on this page). Here's what I have there:
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
I never had this problem before. I somewhat recently upgraded to Lion and am working on a new virtualenv with the bare minimum of modules installed.
Found the answer courtesy of Bradley Ayers:
set "unix_socket_directory" to "/var/pgsql_socket/" in postgres.conf
executed following commands:
"$ sudo dscl . append /Groups/_postgres GroupMembership postgres"
"$ sudo chmod g+w,o+rx /var/pgsql_socket/"
restarted postgresql
your database is not running try
pg_ctl -D /usr/local/var/postgres start
assuming thats where your postgre data directory is

Categories

Resources