7

related posts: 1) docker postgres pgadmin local connection

2) https://coderwall.com/p/qsr3yq/postgresql-with-docker-on-os-x (in the example "Name" entry is not filled in)

there are two ways to complete this task, I use official postgres

METHOD 1:

and runs it with

sudo docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres

then connect with

Name: postgres
Host: localhost
Port: 5432
user
pass
...

METHOD 2:

starts with

sudo docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

and then check the ip of container

sudo docker inspect

say result

172.17.42.1

then connect with pgAdmin tab Properties filled info

Name: postgres
Host: 172.17.42.1
Port: 5432
user
pass
...
Community
  • 1
  • 1
Hello lad
  • 17,344
  • 46
  • 127
  • 200
  • and error itself?.. tried to psql first?.. – Vao Tsun Aug 04 '15 at 05:04
  • 1
    Since you've mapped port 5432 of container to that of host, can't you use pgadmin to connect to port 5432 on the host? What error do you get when you try to connect? – Dharmit Aug 04 '15 at 06:14
  • thanks for the comment. Problem solved by my self, since I have forward the port in Container to local port , I should use localhost or 127.0.0.1 as host address to connect to. I would reedit the question into a guide – Hello lad Aug 04 '15 at 08:34
  • @Hellolad Adding an answer based on my comment so that it can help if someone else stumbles upon this in future. – Dharmit Aug 04 '15 at 09:02

2 Answers2

10

I included this in the docker yaml file to get the database and pgAdmin:

database:
    image: postgres:10.4-alpine
    container_name: kafka-nodejs-example-database
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    expose:
      - "5432"
    ports:
      - 8000:5432
    volumes:
      - ./services/database/schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
      - ./services/database/seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5454:5454/tcp
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@mydomain.com
      - PGADMIN_DEFAULT_PASSWORD=postgres
      - PGADMIN_LISTEN_PORT=5454

The postgres username is alphaone and the password is xxxxxxxxxxx.

Do a docker ps to get the container id and then docker inspect <dockerContainerId> | grep IPAddress

eg: docker inspect 2f50fabe8a87 | grep IPAddress

Insert the Ip address into pgAdmin and the database credentials used in docker:

pgAdmin

Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
6

Since you're mapping the port 5432 on the container to the same port on host with -p 5432:5432 in your docker run statement, try connecting pgadmin to port 5432 on the host instead of the container.

Dharmit
  • 5,498
  • 2
  • 27
  • 30
  • Oh if only it was that simple. It won't take `127.0.0.1` nor `localhost` and of course the port is set to 5432 in the Create - Server dialog [in pgadmin]. I will admit that I do NOT grok the difference between the Container and Host if the port numbers are the same, and I'm specifying the host (/ my machine) in the `Host name/address` field. – Scott Fraley Aug 10 '22 at 23:27