Francisco
Francisco

Reputation: 61

Connecting to Postgresql in a docker container with a GUI

I just started learning docker and I decided to create a postgresql container, and I want to use it as my database.

But the thing is, every time I tried to connect to my postgresql container with my Gui (PostBird), I get an error that says "Connection terminated unexpectedly".

My config file:

postgres:
  image: postgres:alpine
  restart: always
  ports:
    - '3000:3000'
  environment:
    POSTGRES_USER: root
    POSTGRES_PASSWORD: root
    POSTGRES_DB: adonisvue
  volumes:
    - ./init:/docker-entrypoint-initdb.d/

The command I used:

sudo docker-compose up

When my postgres container is running it says "database system is ready to accept connections" but I can't connect with my gui, even using connect url.

I had to change my port to 3000 because docker says that the port 5432 is already in use, but I don't have any container running in it. Is it because of psql?

Sorry, I'm really new to this and I just have a bunch of questions xD

Upvotes: 3

Views: 5834

Answers (1)

Zeitounator
Zeitounator

Reputation: 44615

I had to change my port to 3000 because docker says that the port 5432 is already in use, but I don't have any container running in it. Is it because of psql?

Yes, this is most probably because you have an other postgresql using this port on the local machine. Meanwhile, the postgres instance running inside your container is still using port 5432 (unless you also changed the port inside your container but I'm 99.9% sure you didn't). So the following port setting is wrong.

ports:
  - '3000:3000'

This is mapping port 3000 for any IP configured on your host to port 3000 of your container... with no service running on that one. Try:

ports:
  - '3000:5432'

You can then connect to postgres on port 3000 on your local machine which will forward packets to port 5432 inside the container.

Upvotes: 1

Related Questions