skip to Main Content

I am new to docker and postgres and this is really puzzling to me. After pulling the postgres 14.1 alpine image, I created a container from the image called postgres14.1 setting the user and password:

docker run --name postgres14.1 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:14.1-alpine

Then I created a database called ‘simple_bank’ on the container:

docker exec -it postgres14.1 createdb --username=root --owner=root simple_bank

Before connecting to the database in TablePlus or VSCode with postgres extension, I tried to access in the terminal:

docker exec -it postgres14.1 psql simple_bank

And it works fine. In the terminal , du+ confirms root is indeed a superuser in the simple_bank database:

Role name |                         Attributes                         | Member of | Description 
-----------+------------------------------------------------------------+-----------+-------------
 root      | Superuser, Create role, Create DB, Replication, Bypass RLS | {}        | 

Then I tried to connect via TablePlus with the following config:

Host/Socket: 127.0.0.1
Port:5432
User:root
Password:secret
Database:root

This can’t be connected saying role root doesn’t exit. If leave the user blank and set database to postgres then it connects but it can’t find the simple_bank database created from the command line.

2

Answers


  1. Chosen as BEST ANSWER

    In the end, the problem is because I already have a local instance of postgres installed and it by default also connects to port 5432 on my local machine. Hence in creating the postgres container, I need to choose a different port on local host. So changing the below docker command and then connect locally using port 5433 fixes the problem.

    docker run --name postgres14.1 -p 5433:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:14.1-alpine
    

  2. use host.docker.internal insted of localhost
    basically replace DB_HOST with host.docker.internal

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search