Docker Postgres and Adminer accept connection but doesn't work - postgresql

On my rasperry pi 4 I've installed docker and docker-compose and now I'm tring to install and use Postgres and Adminer
following that https://hub.docker.com/_/postgres I've created docker-compose.yaml file as follow:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
and i run it with
docker-compose -f docker-compose.yaml up -d
after that DB_1 starts and adminer too
but when i try connect to http://192.168.1.38:8080/ i can't reach it
even if i try connect to postgres through pgAdmin it's says
could not connect to server: Connection refused (0x0000274D/10061) Is
the server running on host "192.168.1.38" and accepting TCP/IP
connections on port 5432?
however if i don't use docker-compose but just
docker run --name postgres -d --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=123456 -v ${PWD}/data:/var/lib/postgresql/data postgres
it's work through pgAdmin
do you know what i'm doing wrong?
UPDATE: seems the problem is with docker-compose because any kind of docker-compose.yml file block connection to it...
with a container with djgango i tried to start server and it's works but when i try reach page it seem bloccked too
when i run docker-compose.yaml file docker-compose ps output is:
sudo netstat -tulpn screenshot

PgAdmin can't reach 5432 ports because you don't expose it.
Like for Adminer you need to expose the Postgres port 5432 on your machine in your compose file.
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080

a little late to the party but what you need to do is figure out the IP address of the postgres container, and use that as your host.

Related

can't access Postgres db on Mac when I set port: 5432:5432 but it works fine when I set to 5001:5432

I was setting up docker compose
version: "3.7"
services:
postgres:
container_name: mydevdb
image: postgres:13
restart: always
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- postgres:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres:
my env file
POSTGRES_USER=username
POSTGRES_PASSWORD=password
POSTGRES_DB=dev
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}#localhost:5432/${POSTGRES_DB}?schema=public
and when I run Prisma Migrate dev it logs:
Error: P1010
User `username` was denied access on the database `dev.public`
but when I changed ports in docker-compose.yml to "5001:5432"
and updated my DATABASE_URL port from 5432 to 5001 it works fine .. I just don't know why that happens in my Mac however my ubuntu machines works well with 5432:5432 port
You probably have another instance of postgres running on your mac.
You can run
sudo lsof -PiTCP -sTCP:LISTEN | grep 5432
to check.

Pgadmin does not start with docker-compose-file

I started working with postgres and discovered pgadmin.
I started like this:
Postgres:
$ docker run --name admin -e POSTGRES_PASSWORD=admin -p 5432:5432 -d postgres:latest
Pgadmin:
docker run -p 5050:80 -e "PGADMIN_DEFAULT_EMAIL=admin#admin.com" -e "PGADMIN_DEFAULT_PASSWORD=root" -d dpage/pgadmin4
So this worked perfectly fine, I started the postgres container and afterwarda the pgadmin cointainer and got on the site http://localhost:80/login and could login
The problem now is the docker-compose.yml that I wrote. As I deploy my docker-compose-file, the containers are both running but I can't access the login page of pgadmin
Docker-compose.yml:
version: '3.8'
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
When I run it, it works as it should. So I think your issue is the URL you try to access it on. You need to access it on the mapped port 5050. Not 80, as you've written. So the URL is http://localhost:5050/login.

How to restrict remote access to postgresql docker container?

Using docker-compose I've developed a website which includes a Postgresql database. I deployed it on a server using the same docker-compose.yml file, but I found that I can remotely access my postgres server using psql.
I currently have these lines in my docker-compose
version: '3.6'
services:
db:
image: postgres
ports:
- 5432:5432
environment:
POSTGRES_DB: my_website
POSTGRES_USER: my_website
POSTGRES_PASSWORD: my_password
Does anybody know how I can only allow access to the postgres db from within the docker network so that it cannot accessed from outside the host OS?
At the moment you are binding the port 5432 from within the docker container onto a host port using the ports directive. To only have the port accessible within your local docker network, change ports to expose
version: '3.6'
services:
db:
image: postgres
expose:
- "5432"
environment:
POSTGRES_DB: my_website
POSTGRES_USER: my_website
POSTGRES_PASSWORD: my_password

Pgadmin4 cannot connect to my postgres database

here is my docker-compose.yml file:
version: "3.5"
services:
db:
image: myapp
container_name: my-database
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypasswd
POSTGRES_DB: mydb
expose:
- "5432"
ports:
- 8000:5432
volumes:
- /path/to/my/migrationsV1_0__audit_table.sql
- //path/to/my/migrations/V1_1__tables.sql
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
Then i $ docker inspect xxxxxxxx | grep "IPAddress"
It produces output:
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
After docker-compose I enter values to pgadmin as so:
Pgadmin_capture
Yet pgadmin gives error:
Unable to connect to server
timeout expired.
What is wrong here?
In your configuration PostgreSQL database has 5432 port (it is default value).
Are you sure in this line?
PGADMIN_LISTEN_PORT=5454
Try to replace it with
PGADMIN_LISTEN_PORT=5432
for a workaround to this issue, just run the container with --user=root (in docker-compose use user: root)
You need to use the gateway address of Postgres container. Use docker inspect xxxxxxxx | grep Gateway to get it. I guess in your case it would be 172.17.0.1
In my case, my pgadmin container didn't shared the DB container's network.
So, you should run this command.
docker network inspect NETWORK <YOUR DB CONTAINER'S NETWORK>
And if it doesn't, run this command
docker network connect <YOUR_DB_CONTAINER'S_NETWORK> <YOUR_PGADMIN_CONTAINER'S NAME>

Connecting docker postgres to pgAdmin

Given this docker-compose.yml, am experiencing difficulties connecting the docker stack to my pgAdmin.
version: '3.1'
services:
database:
image: postgres
restart: always
environment:
POSTGRES_USER: db-user
POSTGRES_PASSWORD: db-user
POSTGRES_DB: db
ports:
- 5432:5432
For the pgAdmin Connection properties, here's what I've used (others, default values):
Host: 127.0.0.1
Username & Password: db-user
And for the error message when saving:
Error saving properties: UNAUTHORIZED
Unable to connect to server
The docker postgresql page suggest the stack.yml:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
And then visit http://localhost:8080.
Or:
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" ...
In your case, adding port to the database itself suggests your queries should use that port.