0

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.

1
  • Why do you tell it to connect to root if you want to connect to simple_bank? Commented Feb 20, 2022 at 15:22

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.