Prisma cannot connect to Postgres with PostGIS running inside Docker container - postgresql

I was wondering if someone could help me figure this out.
I have a Node application using a Postgres database with PostGIS running inside a docker container. Whenever Prisma tries to connect to it, I get the error P1001: Can't reach database server at localhost:15432. I can connect to it fine using some database management tools, and I can connect to deployed Postgress database using PostGIS just fine. Below is my docker-compose file and the connection string.
version: '3.8'
services:
postgres:
container_name: test-postgres
image: kartoza/postgis:14-3.3--v2022.08.30
ports:
- 15432:5432
environment:
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=test-db
volumes:
- postgis-data:/var/lib/postgresql
volumes:
postgis-data:
DATABASE_URL="postgresql://admin:secret#localhost:15432/test-db?schema=public"
The schema.prisma file:
generator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

Related

Entity Framework and Docker Container

First of all, I'm sorry for my English.
I am developing an ASP.NET Core Web API, and I am working with a friend. To work together comfortably I wanted to set up a Docker with our API, and a database running. And I have problem with Entity Framework.
Here is the problem, the name of the container for Postgres is postgres_image, so the connection string to connect to it from the api is:
"Host=postgres_image;Port=5432;Database=grdedb;Username=postgres;Password=postgres"
And it is working. But when I want to create an Entity Framework migration to the database located in the Docker container, it doesn't work; I have to change the host to localhost.
But when I change it the migration works and after that it is the API which stops.
Thank you very much for your help
My docker compose file
version: '3.4'
networks:
grdeapi-dev:
driver: bridge
services:
grdeapi:
image: grdeapi
build:
context: .
dockerfile: ./Dockerfile
ports:
- 5101:5101
networks:
- grdeapi-dev
postgres_image:
image: postgres:latest
ports:
- 5432:5432
restart: always
volumes:
- db_volume:/var/lib/postgresql/data
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_DB: "grdedb"
networks:
- grdeapi-dev
volumes:
db_volume:
My connection string
"Host=postgres_image;Port=5432;Database=grdedb;Username=postgres;Password=postgres"
And the error I get from the entity framework
Failed to connect to 212.95.74.75:5432
Entity framework works when I change the host to localhost, but then my dotnet api can't connect to my database.
Have a config for running in Docker, and one for running in development.
In the former (name it appsettings.Development.Docker.json for example) configure your connection string with the Docker hostname ("postgres_image"), and locally (when running from Visual Studio or when running migrations against said database server) just use "localhost". Name the latter appsettings.Development.json.
Then adjust your DOTNET_ENVIRONMENT accordingly:
services:
grdeapi:
DOTNET_ENVIRONMENT: "Development.Docker"

Cogstack NiFi on Windows 10 - Sample DB doesn't get populated

I am trying to get the Cogstack NiFi Docker implementation running on my Windows machine.
https://github.com/CogStack/CogStack-NiFi
When you run the Docker command;
docker-compose -f services.yml up -d samples-db
It is supposed to run this code from the services.yml (https://github.com/CogStack/CogStack-NiFi/blob/master/deploy/services.yml)
services:
#---------------------------------------------------------------------------#
# Postgres container with sample data #
#---------------------------------------------------------------------------#
samples-db:
image: postgres:11.4-alpine
container_name: cogstack-samples-db
restart: always
volumes:
# mapping postgres data dump and initialization
- ../services/pgsamples/db_dump/db_samples-pdf-text-small.sql.gz:/data/db_samples.sql.gz:ro
- ../services/pgsamples/init_db.sh:/docker-entrypoint-initdb.d/init_db.sh:ro
# data persistence
- samples-vol:/var/lib/postgresql/data
ports:
# <host:container> expose the postgres DB to host for debugging purposes
- 5555:5432
#expose:
# - 5432
networks:
- cognet
And thus the init_db.sh (https://github.com/CogStack/CogStack-NiFi/blob/master/services/pgsamples/init_db.sh)
But unfortunately the data does not get populated.
When I try manually running init_db.sh it gives an error;
/docker-entrypoint-initdb.d # ./init_db.sh
Creating database: db_samples and user: test
psql: FATAL: role "root" does not exist
What could be the reason ?

Connect to Postgres container with PhpStorm

I would like to access my Postgres database (docker container) from PhpStorm.
docker-compose.yml
# Run docker-compose build
# Run docker-compose up
# Live long and prosper
version: '3.1'
services:
apache:
build: .docker/apache
container_name: sf-apache
ports:
- 82:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- ${SYMFONY_APP}:/home/wwwroot/sf3
depends_on:
- php
postgres:
container_name: postgres
restart: always
image: 'postgres:12.6'
ports:
- "5432:5432"
environment:
- "POSTGRES_USER=${PGSQL_ADMIN_USER}"
- "POSTGRES_PASSWORD=${PGSQL_ADMIN_PASSWORD}"
volumes:
- ./API/var/postgres:/var/lib/postgresql/data
- .docker/postgresql/init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
My PhpStorm config :
I can access to my database via docker exec -it postgres bash
If php storm is on the same host then you need to use localhost. If both phpstorm and pg is part of the same compose file , then you would use the service name since both would be in the same virtual network
I found the solution.
I have a Postgres local and a Postgres with docker. My Postgres local get the upper hand on my docker Postgres. I have killed the service and put the container service name on it.
Works perfectly.
Thanks for you help,

How do I connect to Docker Postgres Container from an Outside CLI?

I have my Postgres container running, built from this docker-compose file:
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB=db
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password.
It spins up fine & my other dockerized servers can connect to it. But, if I open up a CLI from outside the docker instance & try to connect with
psql postgres://postgres:password#localhost:5432/db
Or try to add a database connection in PyCharm, I get
psql: error: could not connect to server: FATAL: database "db" does not exist
as a response. What do I need to do to allow outside calls to the containerized database? I've tried adding "expose:5432" to the docker compose, but that didnt help.
answered my own question here - by stopping PgAdmin from running, which I guess was blocking the 5432 port, the containerized postgres service could be accessed by the CLI & PyCharm

Fedora: Application Container Cannot establish connection to DB Container

I am having trouble connecting to a db container from the application container on a Fedora Host. I have verified being able to connect to the database using the same credentials via the psql command line interface. Using the same information in my application does not work.
Here is my docker compose file
version: '3.3'
services:
postgrestest:
build: ./vrs
command: python3 app.py
volumes:
- ./vrs/:/appuser/
ports:
- 5000:5000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER={{user}}
- POSTGRES_PASSWORD={{password}}
- POSTGRES_DB=sharepointvrs
volumes:
postgres_data:
This is the code used to connect to the container, from within the application container:
dbconfig = environment["database"]
try:
connection = psycopg2.connect(
dbname=dbconfig["dbname"], #sharepointvrs
user=dbconfig["user"],
password=dbconfig["password"],
host=dbconfig["host"], # tried 0.0.0.0, localhost, and IP address obtained from docker inspect
port=dbconfig["port"] # 5432
)
connection.autocommit = True
except:
print("Database initialization failed.")
I've tried both using localhost and using the IP obtained from running a docker inspect:
# tried 0.0.0.0, localhost, and IP address obtained from docker inspect
In your app's config, set the database host to 'db'
That exists as a DNS alias, available in the other containers, based on what you set the service name to in your compose file:
services:
db:
# ...
The issue was due to firewall interface policies configured by the docker installation on Fedora.
Docker must be added to the trusted zone before it gets installed.
More information here