7

I am new to docker and still learning how to use it, I am trying to use docker-compose to run Django and Postgres together and they run perfectly and the migrate done and everything, but i have a problem i cant connect into the database using pgAdmin4 to look at the database that's my setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'slack',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': 5432,
    }
}

and that's my docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: username
        POSTGRES_PASSWORD: password
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/slack_code
    ports:
      - "8000:8000"
    depends_on:
      - db

everything works seems to be fine :

sudo docker-compose up
slackwebapp_db_1 is up-to-date
Creating slackwebapp_web_1 ... done
Attaching to slackwebapp_db_1, slackwebapp_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   | 
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   | 
db_1   | Data page checksums are disabled.
db_1   | 
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... ok
db_1   | 
db_1   | Success. You can now start the database server using:
db_1   | 
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   | 
db_1   | 
db_1   | WARNING: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | waiting for server to start....2018-01-18 19:46:43.851 UTC [38] LOG:  listening on IPv4 address "127.0.0.1", port 5432
db_1   | 2018-01-18 19:46:43.851 UTC [38] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
db_1   | 2018-01-18 19:46:43.851 UTC [38] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
db_1   | 2018-01-18 19:46:43.853 UTC [38] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-18 19:46:43.864 UTC [39] LOG:  database system was shut down at 2018-01-18 19:46:43 UTC
db_1   | 2018-01-18 19:46:43.867 UTC [38] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | CREATE DATABASE
db_1   | 
db_1   | CREATE ROLE
db_1   | 
db_1   | 
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   | 
db_1   | 2018-01-18 19:46:44.388 UTC [38] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2018-01-18 19:46:44.389 UTC [38] LOG:  aborting any active transactions
db_1   | 2018-01-18 19:46:44.390 UTC [38] LOG:  worker process: logical replication launcher (PID 45) exited with exit code 1
db_1   | 2018-01-18 19:46:44.391 UTC [40] LOG:  shutting down
db_1   | 2018-01-18 19:46:44.402 UTC [38] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   | 
db_1   | PostgreSQL init process complete; ready for start up.
db_1   | 
db_1   | 2018-01-18 19:46:44.501 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-01-18 19:46:44.501 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-01-18 19:46:44.502 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-18 19:46:44.514 UTC [65] LOG:  database system was shut down at 2018-01-18 19:46:44 UTC
db_1   | 2018-01-18 19:46:44.518 UTC [1] LOG:  database system is ready to accept connections
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | 
web_1  | You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | January 18, 2018 - 19:48:49
web_1  | Django version 2.0.1, using settings 'slack_webapp.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
web_1  | [18/Jan/2018 19:56:03] "GET / HTTP/1.1" 200 16559
web_1  | [18/Jan/2018 19:56:03] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
web_1  | [18/Jan/2018 19:56:08] "GET /admin/ HTTP/1.1" 302 0
web_1  | [18/Jan/2018 19:56:09] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1855
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/base.css HTTP/1.1" 200 16106
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/responsive.css HTTP/1.1" 200 17894
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/login.css HTTP/1.1" 200 1203
web_1  | [18/Jan/2018 19:58:58] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
web_1  | [18/Jan/2018 19:58:58] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/base.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 412
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/responsive.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 200 331
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
web_1  | [18/Jan/2018 19:59:05] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:59:07] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:59:11] "GET /admin/ HTTP/1.1" 200 2988
^CGracefully stopping... (press Ctrl+C again to force)
Stopping slackwebapp_web_1 ... done
Stopping slackwebapp_db_1  ... done

but still i cant connect and i don't know how to set up a password for the Postgres default user like we do in

sudo docker run --name test -e POSTGRES_PASSWORD=password -d postgres

cuz i cant do the same with docker-compose i guess, Thanks in advance.

Snowflake
  • 191
  • 1
  • 2
  • 13
  • This answer below fixed it for me: https://stackoverflow.com/a/62230446/3559330 - changing the hostname to "db", the name of the database service. – mattyb Mar 04 '21 at 23:20

5 Answers5

34

Hostname should be name of service defined in your docker-compose.yml

This is because you're in the docker network

You cannot use localhost or 127.0.0.1 here, because pgadmin is in container, and localhost here means 'pgadmin container'.

Let's consider your case:

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepclose

  pgadmin:
    image: chorss/docker-pgadmin4
    ports:
      - 5050:5050

In this case,

Hostname: db

port : 5432

user: snowflake

pass: 1Stepclose

Hope this helps :)

3

In order to access the postgres database from an external program you will need to mount port 5432 which is exposed by the postgres service to a port on your host. With the following changes to your docker-compose.yml file you should be able to connect to the database using pgadmin (on localhost:5432) as well as have postgres create your user for you.

db:
  image: postgres
  ports:
  - "5432:5432"
  environment:
    - POSTGRES_DB=slack
    - POSTGRES_USER=snowflake
    - POSTGRES_PASSWORD=1Stepclose

Edit: I did not realize that you were trying to connect with pgadmin4 running in another docker container. The easiest way to set that up to allow pgadmin4 container to communicate with the postgres container is to add pgadmin as a service in your docker-compose.yml file. Update your docker-compose.yml file to contain the following configuration:

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepclose

  pgadmin:
    image: chorss/docker-pgadmin4
    ports:
      - 5050:5050

  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/slack_code
    ports:
      - "8000:8000"
    depends_on:
      - db

Browse to localhost:5050 > Click add new server > Enter any name > Click on connection tab > Enter db for Hostname/Address > Enter snowflake for username > Enter 1Stepclose for password > Click save

Dovid Gefen
  • 323
  • 3
  • 17
  • I tried your method still didn't work, is that something becuz of docker-compose maybe! – Snowflake Jan 18 '18 at 23:14
  • Run `sudo apt-get install postgresql-client` on host (while docker-compose up is running), then run the following command from the terminal `psql -U snowflake -h localhost -p 5432 -d slack` if that works then it means that your pgadmin4 program is not configured correctly. – Dovid Gefen Jan 19 '18 at 10:32
  • By the way when you set both the POSTGRES_USER and POSTGRES_PASSWORD variables you are creating a new postgres user and are not setting default postgres user's password, only if you just set POSTGRES_PASSWORD variable does it then set it as the password for the default user. – Dovid Gefen Jan 19 '18 at 12:18
  • i have set POSTGRES_PASSWORD only but also seem not to work correctly, however, i gave up and used it as it is, as the main reason for all this was learning Django, not docker but i thought its good idea to have a well set up env would be good. – Snowflake Jan 19 '18 at 16:23
  • I have updated my answer. If it works please mark my answer as correct. – Dovid Gefen Jan 20 '18 at 23:15
  • sorry mate, still no luck :( "Could not connected to server(#1) - 'dasda'. Error: could not translate host name "db" to address: Name does not resolve" – Snowflake Jan 21 '18 at 13:02
  • Try running `docker-compose down -v` to fully remove any previous configurations, then run `docker-compose up` again to start from fresh. – Dovid Gefen Jan 21 '18 at 13:58
0

In your setting.py file you are calling the db host 'db' but in your compose output it appears to be called 'slackwebapp_db_1'. Try changing setting.py to the full name.

JohnK
  • 16
  • 1
  • 'slackw3bapp_db_1' is the container name mate and its auto generated by docker-compose, plus Django and postgres are communicating well, i just wanna use 3rd party app to look at the db to know what i am doing – Snowflake Jan 19 '18 at 07:39
0

I have used Pycharm to control my project, and the built-in database management system in Pycharm has connected successfully, maybe it was something wrong with pgadmin4 then, i was using chorss/docker-pgadmin4 image for pgAdmin4 as i am linux so maybe it was something wrong with the image or something. thanks guys for your effort.

Snowflake
  • 191
  • 1
  • 2
  • 13
-1

I looked at postgres, from which I found the git repository (according to your docker-compose, you use the latest tag). Looks like the default user name and password 'postgres' are hard-coded. You might want to try 'postgres' in settings.py for user name and password, and see if that works.

Chun-Yen Wang
  • 568
  • 2
  • 10
  • I tried it but unfortunately doesn't work too, the strange thing is when i load the posgress container using: "sudo docker run --name test -e POSTGRES_PASSWORD=1Stepcloser -d postgres" it works but when using docker-compose i cant connect. – Snowflake Jan 19 '18 at 08:12
  • sorry for my previous nonsense answer. i tried similar thing and this should work. one difference is that i don't launch the runserver command in docker-compose.yml. i started docker-compose with -d, and then attached to db container to check the environment vars present. then i attached to web container and ran migrate command. i noticed the database slack was created in db container, so the connection should work. i notice that in your log output there is complaint that you should do migrate command as well. maybe you should do that... – Chun-Yen Wang Jan 19 '18 at 17:43