How to connect to OpenMapTiles Docker Postgres DB - postgresql

I am currently playing around with openmaptiles (https://github.com/openmaptiles/openmaptiles) and I'd like to figure out how to import my own data into the resulting mbtiles. But first I'd like to look at how the postgres database it is using is structured.
I just can't figure out how I can connect to the postgres database using my GUI tool I have running locally.
I start postgres using the command provided on the help page:
docker-compose up -d postgres. Is it just not visible to outside of the docker container (I am also very new to docker)?
And is there a way to make it visible to my local system?

docker-compose up -d postgres refers to this part of the docker-compose.yaml file:
services:
postgres:
image: "openmaptiles/postgis:2.9"
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- postgres_conn
ports:
- "5432"
env_file: .env
...
As you can see in the ports: section, there is no container - host port mapping here. To access this postgres database from your host try using "5432:5432". (notice that if you're already using this port on the host, you will have to pick an available one).
For more information on the docker-compose file reference and ports, check the docs.

Related

Can't connect to the Postgres Docker container using SqlAlchemy

I have a Postgres Docker Container running locally, and the docker compose code for it looks like this
version: '3.9'
services:
db:
image: "postgres"
container_name: db
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=dbname
The database is started using the docker compose run db command
I then find the IP address of the container once it's running, which is "192.168.240.2"
When I try to connect to the database with SqlAlchemy like the following in a python program (this is on the same computer but outside of the container)
import sqlalchemy
engine = sqlalchemy.create_engine('postgresql://postgres:password#192.168.240.2:5432/dbname')
engine.connect()
It shows me this error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "192.168.240.2", port 5432 failed: Operation timed out
Is the server running on that host and accepting TCP/IP connections?
Anyone knows what the problem is here? thanks!
Tried searching for the error message, but changing the input to the create_engine() function according to other posts various ways still result in the same problem. However, I still imagine something's off with the string?

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.

What am I doing wrong in docker-compose for .netcore and postgres?

I am banging my head for a while on this issue and can't find what the issue might be. Running Docker Desktop on Windows 10. I have one dotnetcore 3.1 api that connects to postgres. Both of these are being run in containers.
Everything seems to work except connection to the database. Since I looked at my docker-compose.yml milion times, I can't come up with any other idea.
Here is my connection string:
"Server=postgres;Port=5432;Database=IdentityManager;User Id=postgres;Password=12345678;"
Here is docker-compose.yml file:
version: '3'
services:
identityserver:
depends_on:
- "postgres"
container_name: identityserver
build:
context: ./my_project/
dockerfile: Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT='Development'
ports:
- "5000:80"
postgres:
image: "postgres"
container_name: "postgres"
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=12345678
- POSTGRES_DB=IdentityManager
expose:
- "5432"
Everything builds up, but connection to database fails:
Unhandled exception. Npgsql.NpgsqlException (0x80004005): Exception while connecting identityserver
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address [::1]:5432
The weirdest thing is that when I run postgres alone with this same configuration on docker-compose.yml, and run the application outside of container with slightly different connection string:
"Server=127.0.0.1;Port=5432;Database=IdentityManager;User Id=postgres;Password=12345678;"
I am able to connect to database.
I tried cleaning everything docker system prune -a, tried restarting Docker, restarting PC, but to no awail. Can anyone try to help?
Finally, I was able to resolve my own problem and it wasn't in the docker-compose.yml file at all. Somewhere in the application code, connection string was changed to look for localhost as a host instead of postgres.
After changing it back to postgres, everything was fine.
try to
links:
- postgres
Maybe it will help

how to interpret db volume entries in a docker compose file

I'm in the process of setting up my first postgresql docker container, as part of a distributed application using docker compose, and am somewhat confused on the syntax of the compose file. Still pretty new to docker so forgive me if this is straight out of docker 101.
Here's what it looks like for my postgres container in docker-compose.yml:
version: '2'
services:
database:
image: postgres
container_name: database-container-name
environment:
- POSTGRES_PASSWORD=some_password_here
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- PGDATA=/var/lib/postgresql/data/db-files/
ports:
- 5433:5432
volumes:
- ./.db/data:/var/lib/postgresql/data:delegated
- ./.db/init:/docker-entrypoint-initdb.d
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder? I've looked at some documentation but it's not sinking in.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder?
In the volumes section of your database service, you're creating a couple of bind mounts between your host and your container. The syntax of each volume entry (in this situation) is HOST_PATH:CONTAINER_PATH:OPTIONS. So when you see:
- ./.db/data:/var/lib/postgresql/data:delegated
You are mapping the local (to your docker-compose.yml) .db/data directory onto /var/lib/postgresql/data in the container. In other words, changes made in one directory will be visible in the other.
The delegated option is specific to MacOS; the docs say:
delegated: The container runtime’s view of the mount is authoritative. There may be delays before updates made in a container are visible on the host.
The volume section of your service corresponds to the -v option to docker run; you may find more interesting in the docker run documentation in addition to the bind-mount docs I linked above.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The ports section is for publishing ports on your host. The syntax is HOST_PORT:CONTAINER_PORT. So when you see:
- 5433:5432
This is mapping host port 5433 to container port 5432. In other words, you can connect to your postgres database by connecting to port 5433 on your host. This is probably in place to avoid a conflict if you already had a postgres instance running on your host, which would already be bound to port 5432.

Can't set Password for Postgres using docker-compose

I am not able to set Password for Postgres using Docker-compose. Postgres is loading without password and with the default user name "postgres", non of the environment variables below seems to applied. below is the db service of my docker-compose.yml file: (version 3)
db:
image: postgres
container_name: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: db
restart: unless-stopped
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
Note I tried using the "-POSTGRES_USER=" as well, it didn't work
Also, I deleted all old containers/volumes.
Any idea?
The problem should be with the volume attached. When your container start it will add the credentials you give him, but then the volume will be attached and that will cause this information being rewritten.
For more information have a look at https://github.com/docker-library/postgres/issues/203#issuecomment-255200501.
The main reason being use of ':' instead of "=" in the environment section.
Ideally it should look like this:
db:
image: postgres
container_name: postgres
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD= pass
- POSTGRES_DB= db
restart: unless-stopped
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
Your configuration works fine for me. I suspect you are not using the complete set of correct credentials, which includes the username, password, and database name. If I take your example docker-compose.yaml and run it without modifications, I can connect to the database db like this with username user and password pass:
$ psql -h localhost -U user db
Password for user user:
psql (9.5.7, server 9.6.1)
WARNING: psql major version 9.5, server major version 9.6.
Some psql features might not work.
Type "help" for help.
db=#
Had the same issue.
Couldn't solve it for 2 weeks.
Read almost everything related to it.
And after I finished all PosgreSQL server related processes on local machine, everything goes well.
Start postgres instance:-
docker run --name postgres-0 -e POSTGRES_PASSWORD=mypassword -p 5433:5433 -d postgres
Now we can check docker all running container by this command:-
docker ps