I am using PostgreSql as database in my Django project. When I run it locally via pgAdmin, the data appears, but when I run it on the server, the data does not come in pgAdmin.
Django settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'xxxx',
'USER' : 'xxx',
'PASSWORD': 'xxx',
'HOST': 'pgdb',
'PORT': 5432,
}
}
docker-compose.yaml
services:
# CaddyServer reverse proxy
caddy:
restart: always
image: caddy:2-alpine
ports:
- "443:443"
command: caddy reverse-proxy --from https://xxxxxx.com --to http://0.0.0.0:8000
#volumes:
# - /local/path/to/Caddyfile:/path/inside/continer/to/Caddyfile
# networks:
# - web
# - bridge
# Django web app
django:
restart: always
build: .
ports:
- "80:8000"
depends_on:
- pgdb
#environment:
# - url=https://api.backend.example.com
#command: "gunicorn config.wsgi:application --bind 0.0.0.0:8000"
#networks:
# - bridge
pgdb:
image: postgres
container_name: pgdb
environment:
- POSTGRES_DB=xxxx
- POSTGRES_USER=xxxx
- POSTGRES_PASSWORD=xxxx
volumes:
- pg-data:/var/lib/postgresql/data/
volumes:
pg-data:
I am connecting via pgAdmin server ip. The pgAdmin connection is established, but the data I entered into the system is not visible on pgAdmin.
That's because it's not inside your postgres database, but only in pg_admin
What I'd suggest you to do is to run this command via ssh to get a list of databases:
psql -l
Once you get all the db names, and its owners, check if your db appears. If it does, try accessing it with:
psql -U your_db_username || psql -U your_db_username -d your_db_name
Access and query your tables to see if the data is indeed there.
If it's not there, what I would suggest is to dump the db with:
pg_dump -U db_username db_name > your_backup.pgsql
Afterwards, you should restore that dump through the terminal into postgres:
psql -U db_username --set ON_ERROR_STOP=on -f your_backup.pgsql
Let me know if this helped you. I can further assist you on this one if it does help in the end but you face new/other problems.
Related
This question already has answers here:
Connecting to Postgresql in a docker container from outside
(17 answers)
Closed 8 months ago.
I start learning docker with Django+PostgreSQL, I did all steps from there https://docs.docker.com/samples/django/
How can I open created database on terminal or pgAdmin to see tables and change them?
I use: Ubuntu 22.04 / Docker Engine 20.10.17 / Docker Compose version v2.6.1 / psycopg2-binary==2.9.3 / Django==4.0.6 / psql (13.7 (Ubuntu 13.7-1.pgdg22.04+1))
my docker-compose.yml:
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('POSTGRES_NAME'),
'USER': os.environ.get('POSTGRES_USER'),
'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
'HOST': 'db',
'PORT': 5432,
}
}
some info if needed, postgres file created are owned by root:
postgres file 1/2
postgres file 2/2
docker-compose commands
Thanks for answers!
Have you tried this?
docker exec -it {your postgres container name} psql -U {your db username} {your db name}
or simply
docker compose exec {service name} manage.py dbshell
django.db.utils.OperationalError: connection to server at "db" (172.18.0.2), port 5432 failed: FATAL: the database system is starting up
I have an issue connecting to the Postgres contaner. I was trying in different ways like setting passwords only in the docker-compose file. Still am stuck.
docker-compose.yml
version: '3'
services:
db:
image: postgres:alpine
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=user
- POSTGRES_DB=userdb
volumes:
- django_db:/var/lib/postgresql/data
container_name: db
singup:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
container_name: singup
depends_on:
- db
volumes:
django_db:
database setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'userdb',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'db',
}
}
This is a timing issue, even thought you have the below line
singup:
depends_on:
- db
It's just waiting for db container to be up and running, not neccessarily Postgres.
To avoid this, use a tool such as wait-for-it, dockerize, sh-compatible wait-for, or RelayAndContainers template. These are small wrapper scripts which you can include in your application’s image to poll a given host and port until it’s accepting TCP connections.
For example, to use wait-for-it.sh or wait-for to wrap your service’s command:
singup:
depends_on:
- db
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
Repo of wait-for-it: https://github.com/vishnubob/wait-for-it
I'm relatively new to Django and Docker and am following a tutorial to build a mini application. However, I'm getting stuck with the following error:
django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known
My docker-compose file looks as follows:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- db
links:
- db:db
My settings.py file contains the database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
I've seen the post here and here however both have not fixed the issue.
Would appreciate some guidance. Thanks.
So you are trying to reach the db which is running in one container from another container? If yes - the following could potentially help, at least it helped me when I had similar issues.
Try to define networks config in addition to links in your compose file, create a network with some name and define it in both services. Like described here, as the docks on links config recommend to do that.
Something like this for your case:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432'
networks:
some_network:
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- db
links:
- db:db
networks:
some_network:
networks:
some_network:
It helped me to resolve the host name to connect to the db.
I had to use docker-compose up to make sure my db container was running.
Instead of redefining the networks, use
docker-compose down -v
to stop all containers and remove all cached data.
Then
docker-compose up
to restart from scratch.
I've been searching for several days for the solution to this issue. Here's what I did:
1 - I copied the postgresql.conf.sample file from my postgres container to my project folder
you must get in to cd usr/share/postgresql
with docker exec -it yourcontainer bash
2 - I changed its name to postgresql.conf
3 - I modified these variables in postgresql.conf
listen_addresses = '*'
port = 5432
max_connections = 100
superuser_reserved_connections = 3
unix_socket_directories = '/tmp'
4 - I added it to my Dockerfile
COPY postgresql.conf /tmp/postgresql.conf
5 - settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test',
'USER': 'testing',
'PASSWORD': 'tests',
'HOST': 'localhost',
'PORT': 5432,
}
}
6 - I did docker-compose up -build again
I came across this issue recently, i solved it by adding postgres environment variables to db in my docker-compose.yml file like so
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432'
environment:
- POSTGRES_DB=booksdb
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
volumes:
- .:/code
depends_on:
- db
links:
- db:db
In my specific case, I had postgres without any version, so postgres image gets updated and the db (postgres) container won't get up
The following configuration works for me from official documentation
version: "3.8"
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
then run those commands:
docker-compose up -d --build
docker-compose logs
docker exec -it "container_name" python3 manage.py migrate
This works for me. Make sure of the path to the manage.py file is correct.
Trying to run my django server in a docker, but the postgres port is already being used? When I run "docker-compose up", I receive this error:
django.db.utils.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?
ERROR: Service 'web' failed to build: The command '/bin/sh -c python manage.py migrate' returned a non-zero code: 1
sudo service postgresql status
returns:
9.6/main (port 5432): online
sudo lsof -nP | grep LISTEN
returns:
postgres 15817 postgres 3u IPv4 1022328 0t0 TCP 127.0.0.1:5432
I tried to run "sudo kill -9 15817", but docker-compose up still receives the same error.
Docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'stemletics',
'USER': 'stemleticsadmin',
'PASSWORD': 'changeme',
'HOST': '127.0.0.1', # set in docker-compose.yml
'PORT': 5432 # default postgres port
}
}
In order to use postgres inside of Docker you will need to configure information like the database user, password and db-name. This is done through setting environment variables for the container. A complete list of supported variables can be found here.
Additionally you will want to expose port 5432 of postgres to your web service inside your docker-compose file.
Something like this should work:
docker-compose.yml
version: '3'
services:
db:
image: postgres
ports:
- "5432"
environment:
- POSTGRES_DB=stemletics
- POSTGRES_USER=stemleticsadmin
- POSTGRES_PASSWORD=changeme
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
You will also have to change the hostname you are using inside settings.py. docker-compose creates a default network for your services and attaches the running containers to this network. Inside your web container the database will be available at the hostname db.
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'stemletics',
'USER': 'stemleticsadmin',
'PASSWORD': 'changeme',
'HOST': 'db', # set in docker-compose.yml
'PORT': 5432 # default postgres port
}
}
Lastly if you do not have any sort of database reconnection logic in your python code the migration may still fail. This is due to the fact that depends_on only waits for the container to start, but postgres will take a couple of seconds to initialze after the container is running.
In order to get around this quickly it will be easiest to run one container at a time.
i.e.:
$ docker-compose up -d db
Wait for postgres to initialize
$ docker-compose up -d web
Hopefully this gets you up and running.
I was able to fix this issue simply building my db container, wait few seconds, then building the web container:
docker-compose up -d --build db
wait a few seconds
docker-compose up -d --build web
I hope this helps
I face the same problem to connect the PostgreSQL server to the windows operating system. Then I apply the following way. I hope it will help to solve this problem...
Download postgres_sql
Install postgresql
Search option open 'SQL Shell (psql)'
Create database
Settings add DATABASES....
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'stemletics',
'USER': 'postgres', #default user name
'PASSWORD': 'changeme',
'HOST': '127.0.0.1', # default host
'PORT': '5432', # default postgres port
}
}
I ran into this issue and it turned out that I hadn't started docker desktop. Apparently, if docker desktop is not running, any docker commands you use do not apply to docker desktop but rather a different version of docker on your system. My OS is ubuntu 22.04.
I have a Django project running in multiple Docker containers with help of docker-compose. The source code is attached from directory on my local machine. Here's the compose configuration file:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432:5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8001:8000'
volumes:
- .:/code
depends_on:
- db
Although the application starts as it should, I can't run migrations, because every time I do manage.py makemigrations ... I receive:
django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known
Obviously I can open bash inside the core container and run makemigrations from there, but then the migration files are created inside the container which is very uncomfortable.
In my project's settings the database is configured as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': '5432',
}
}
As docker postgres image is accessible at localhost:5432 I tried changing database host in settings to:
'HOST': '0.0.0.0'
But then when firing up the containers with docker-compose up I'm receiving:
...
django.db.utils.OperationalError: could not connect to server:
Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
...
How should I configure database in settings.py so that Django can access it to create migrations?
Your docker-compose configurations are not correct. You forgot to link services
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432:5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8001:8000'
volumes:
- .:/code
depends_on:
- db
links: # <- here
- db