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
...
4
  • and error itself?.. tried to psql first?.. Commented Aug 4, 2015 at 5: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? Commented Aug 4, 2015 at 6: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 Commented Aug 4, 2015 at 8:34
  • @Hellolad Adding an answer based on my comment so that it can help if someone else stumbles upon this in future. Commented Aug 4, 2015 at 9:02

3 Answers 3

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:
      - [email protected]
      - 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

Sign up to request clarification or add additional context in comments.

1 Comment

I followed the steps but this is connecting to my local postgres server. Not the docker database.
7

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.

1 Comment

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.
0

if you have PostgreSQL and PgAdmin in docker containers, you need to use containername instead of IP or localhost. Example docker-compose

version: "3.8"

services:
  postgres_16.1:
    image: postgres:16.1
    restart: always
    environment:
      - POSTGRES_USER=xxx
      - POSTGRES_PASSWORD=xxx
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
      
  pgadmin:
   image: elestio/pgadmin:latest
   environment:
     PGADMIN_DEFAULT_EMAIL: xxx
     PGADMIN_DEFAULT_PASSWORD: xxx
     PGADMIN_LISTEN_PORT: 5454
   ports:
     - 5454:5454/tcp

volumes:
  postgres-data:
    external: true

So to access pgAdmin you need to open http://localhost:5454 To access Db from pgAdmin you need to in connection put

Host name/address - postgres_16.1 // name of container
port - 5432 // port you open for DB access

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.