I am not sure as to why I cannot connect my postgres client in my docker container from OUTSIDE my docker container.
docker-compose setup
db:
container_name: postgres-container
image: postgres:latest
ports:
- "5432:5432"
environment:
- POSTGRES_USER=liondancer
- POSTGRES_PASSWORD=postgres
volumes:
- ../data/postgres:/var/lib/postgresql/data
With my container running via docker-compose
$ docker exec -it 451b psql -U liondancer
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type "help" for help.
liondancer=# l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------------+------------+----------+------------+------------+---------------------------
liondancer | liondancer | UTF8 | en_US.utf8 | en_US.utf8 |
journey_development | liondancer | UTF8 | en_US.utf8 | en_US.utf8 |
journey_test | liondancer | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | liondancer | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | liondancer | UTF8 | en_US.utf8 | en_US.utf8 | =c/liondancer +
| | | | | liondancer=CTc/liondancer
template1 | liondancer | UTF8 | en_US.utf8 | en_US.utf8 | =c/liondancer +
| | | | | liondancer=CTc/liondancer
(6 rows)
Here is the output of docker ps
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
451b08a85664 postgres:latest "docker-entrypoint.s…" About an hour ago Up 19 minutes 0.0.0.0:5432->5432/tcp postgres-container
However, I want my rails
server and pgAdmin
(currently NOT in container) to be able to communicate with the postgres docker container client. I thought if my rails server
, pgAdmin
, or psql
client connected to 0.0.0.0:5432
, I should be able to connect to the docker container client.
My attempts to connect have been
$ psql -h 0.0.0.0 -p 5432 -U liondancer -d journey_development
psql: error: FATAL: database "journey_development" does not exist
$ psql postgresql://liondancer:postgres@localhost:5432/
psql: error: FATAL: database "liondancer" does not exist
$ psql postgresql://liondancer:postgres@localhost:5432/journey_development
psql: error: FATAL: database "journey_development" does not exist
In rails database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch("POSTGRES_HOST") { "0.0.0.0" } %>
port: <%= ENV.fetch("POSTGRES_PORT") { "5432" } %>
username: <%= ENV.fetch("POSTGRES_USER") { "liondancer" } %>
password: <%= ENV.fetch("POSTGRES_PASSWORD") { "postgres" } %>
development:
<<: *default
database: journey_development
test:
<<: *default
database: journey_test
2
Answers
Try adding this to the environment variables:
To access from outside the container try this command:
You can checkout my blog post for a full setup with Node TS, Postgres and Docker for dev and prod builds.
https://yzia2000.github.io/blog/2021/01/15/docker-postgres-node.html
You can have an init script for creating the database and mount that init script to the postgres container.
Example –
Create an
init.sql
file in the same directory as thedocker-compose.yml
file with the following contents –This
init.sql
script is executed whenever the container is created and a database will be created for you.[Optional]
You can also create an user inside the
init.sql
using –