How to connect to postgresql instance in with Docker Toolbox with pgadmin4 client - postgresql

i'm new to Docker, and i'm running it with Docker Toolbox cause i have win 10 family.
I have a training project where there is a database to put up with docker-compose.
Here is the docker-compose.yml (it's given to me, but i can modify it).
version: '2'
services:
myerp.db:
image: postgres:9.4
ports:
- "127.0.0.1:9032:5432"
volumes:
# - "./data/db:/var/lib/postgresql/data"
- "./init/db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d"
environment:
- POSTGRES_DB=db_myerp
- POSTGRES_USER=usr_myerp
- POSTGRES_PASSWORD=myerp
When i run that through Docker with docker-compose up it seems ok,
but i'm unable to connect to it through a server in pgadmin4
I'm not sure of the connexion setup i'm suppose to put because of Docker ToolBox, where in general to access the running instance you don't use 127.0.0.1 but 192.168.99.100 by defaut.
Anyway, if i configure a postgresql server with
Hostname: 127.0.0.1
Port: 9032
Username and password as written up, i have
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 9032?
If i change the IP in docker-compose for 192.168.99.100, i manage to connect,
but the database is not initialized although there are sql files in the folder
\init\db\docker-entrypoint-initdb.d :
01_create_schema.sql
02_create_tables.sql
21_insert__data_demo.sql
So is it my connexion that is wrongly setup ?
Or should i do it with 192.168.99.100, and it's the initialisation setup that is wrong in the docker-compose ?
Thanks for help !

Ok so i solve the problem, but unfortunately without Docker Toolbox.
I bought a key for windows 10 pro (6€), upgraded the system really fast, and installed Docker Desktop.
Everything worked instantly.
Before the connexion ip was indeed 192.168.99.100, but i don't know why the sql scripts where not copied in the virtual environment, and the folder
/docker-entrypoint-initdb.d
remained empty.
So my solution was to upgrade the system, and everything is fine !

Related

Error: P1001: Can't reach database server at `localhost`:`5432`

I'm having a problem when running the npx prisma migrate dev command. Docker desktop tells me that the database is running correctly on port 5432 but I still can't see the problem.
I tried to put connect_timeout=300 to the connection string, tried many versions of postgres and docker, but I can't get it to work.
I leave you the link of the repo and photos so you can see the detail of the code.
I would greatly appreciate your help, since I have been lost for a long time with this.
Repo: https://github.com/gabrielmcreynolds/prisma-vs-typeorm/tree/master/prisma-project
Docker-compose.yml
version: "3.1"
services:
postgres:
image: postgres
container_name: postgresprisma
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=santino2002
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Error:
Error: P1001: Can't reach database server at localhost:5432
Please make sure your database server is running at localhost:5432.
Docker ps show this:
Looks like the application and the database are running on two separate containers. So, in this case, connecting to localhost:5432 from the application container will try to connect to 5432 port within that container and not in the docker host's localhost.
To connect to database from the application container, use postgres:5432 (If they are on the same network) or <dockerhost>:5432.
Your docker ps output is showing that your postgres container has no ports connected to your local network.
It should look something similiar to this on ports column.
0.0.0.0:5432->5432/tcp, :::5432->5432/tcp
But yours is just 5432/tcp
You need to open ports for your postgres container.
Your docker-compose.yml file you posted in the question is correct. Probably you started postgres container with no ports first, then changed your docker-compose.yml file to have ports. So you just need to restart it now.
Use docker compose down && docker compose up --build -d to do that.

How can i see PostgreSQL UI in pgAdmin if the db is in Docker?

I have installed one open-source project from docker and that project has a database in PostgreSQL. Now I want to see the database from pgAdmin 4 and when I am trying to connect with "host.docker.internal" I am getting an error see image below.
Simply do mapping port for your PostgreSQL (which is running inside Docker). and from your pgAdmin4 you'll connect using localhost:<mapped_port>
If you're using docker-compose
services:
postgre:
ports:
- "8080:5432"
If you're using docker-cli
docker run postgre -p 8080:5432
Then from pgAdmin4 connect to your database using localhost:8080
I suspect this is a networking issue.
If port mapping does not work as suggested in other answers, try specifying the network as host for your container
If you are using docker run
--net=host
Or use the following on docker compose
network_mode: 'host'

Docker Compose Postgres always ended by timeout or connection refused

Got a small question here. I suppose I've done something wrong at a moment but i can't find where and it's been +2 hours I'm turning around.
So Basically, I've created a docker-compose with Postgis (Postgres). I wanted to connect on it through Tableplus.
However, I can't ...
2 kind of error keep appearing :
When I try to connect basically on 127.0.0.1, it's keep telling me connection refused
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
When I try to use the docker IPAddress - 172.23.0.2 (docker inspect the image's ID to get the IP address of the image)
could not connect to server: Operation timed out
Is the server running on host "172.23.0.2" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose.yml
version: '3.5'
services:
db:
image: kartoza/postgis:12.1
environment:
- POSTGRES_USER=user1
- POSTGRES_PASSWORD=password1
- POSTGRES_DB=database_db
volumes:
- data_db_volume:/var/lib/postgresql/12
ports:
- "5432:5432"
volumes:
data_db_volume:
At first, when I tried to connect, it was telling me: role user1 doesn't exist.
So to stop this I ran: brew services stop postgresql on my machine
I think a psql was running locally on the same port because with lsof -n -i:5432 | grep LISTEN i keep having information (it stop since I ran stop Postgresql)
Alright so after few days of research and trying on another computer, it seems it was coming from 2 points: the new docker software with a graphic interface that I didn't use before, once this running correctly and a prune done from this place everything started to work fine so I think something was causing error due to an outdated piece of software.
Thank's everyone

Docker container, running PostgreSQL not allowing php's pg_connect from another container

I have the following architecture:
A network with 3 containers:
container_db, running PostgreSQL
container_pg_admin, running a simple pgAdmin environment
container_php_dev, running the whole environment, needed for the application to function properly (nginx, php, angular for the frontend, etc.)
When I try to pg_connect() from container_php_dev to container_db the connection gets refused. This is the actual error message I get: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: No route to host↵ Is the server running on host "container_db" (172.18.0.4) and accepting↵ TCP/IP connections on port 5432?.
However, when I try to connect to container_db from container_pg_admin I face no problem achieving this (host is container_db and port is 5432). Even if I map port 5432 of container_db to 5555 on host, I'm able to connect to 127.0.0.1:5555 from DBeaver or other DB Manager (TeamSQL).
All containers were restarted a few times as well as docker itself. And as this is my development machine, the host PC was restarted as well.
What might be the problem, how to diagnose and possibly solve? Have any of you experienced such strange docker behaviour?
Try running the container_db in the hosted network using flag --network host

Connecting to the local postgres database from the Docker container

My task was to write a script that received information from a remote computer and recorded the received information on a locally located Postgres DataBase. Tests were successful. The data came, the database was updated.
The next step was to place this script in the docker container. Dockerfile and Docker-Compose.yml are attached below. I add environment with working programs to the container through Volume. The docker container runs on the local machine where the postgres data base is located. When I run the image, I control the workflow of the program and watch the data being received from the remote computer, but writing messages to the postgres data base at the address - postgres + psycopg2: // postgres: hi123 # localhost: 5432 / Back does not occur.
How can I solve the problem of connecting the docker container to a locally located database?
=---------Dockerfile---------=
FROM python:latest
WORKDIR /backend
COPY requirements.txt /backend/
RUN pip install -r requirements.txt
=----------------------------=
=-----Docker-compose.yml-----=
version: '3'
services:
back:
image: git_rep_2_back:latest
environment:
- PYTHONPATH=/backend/
volumes:
- "./Platform:/backend"
command: bash -c "cd server && python launcher.py"
=-----------------------------=
=-------configfile_for_connection_to_database-------=
postgres+psycopg2://postgres:hi123#localhost:5432/Back
=---------------------------------------------------=
The problem is that you are trying to connect to "localhost" from within the container. In colloquial terms, "localhost" refers to "this computer" or "the computer I am working on". All requests to localhost will be redirected back to any processes listening on the localhost interface. Inside a container, the localhost interface is different from that of localhost outside the container. In this case, you should consider making your database either listen on 0.0.0.0 or your machine's IP address. Then, from within the container, you can connect to your database using your machine's IP address.
Hope this helps!