Docker: PostgresSQL - An error occurred using the connection to database - postgresql

I am trying to run a demo ASP.NET Core with PostgresSQL in Docker, but the Database connection is failing.
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database 'ToDoApp' on server
'tcp://postgresserver:5432'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for
context type 'Postgress_ASPNETCore.Db.ToDoContext'.
Npgsql.PostgresException (0x80004005): 3D000: database "ToDoApp" does
not exist
appsettings.json
"ConnectionStrings": {
"ToDoDb": "User ID=postgres;Password=password;Host=postgresserver;Port=5432;Database=ToDoApp;Pooling=true;"
}
docker-compose.yml
version: '2'
services:
aspnetcore:
build:
context: .
dockerfile: Dockerfile
links:
- postgresserver
depends_on:
- "postgresserver"
ports:
- "5000:5000"
networks:
- todoapp-network
postgresserver:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: password
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- todoapp-network
networks:
todoapp-network:
driver: bridge
volumes:
pgdata:
I am unable to figure out the issue. Please help!
Thanks in advance!

The problem is that the ToDoApp database is not created on startup.
To do so you can add it as an environment variable to your docker-compose.yml
version: '2'
services:
...
postgresserver:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: ToDoApp
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- todoapp-network
...

Related

knex connection string with docker compose fails to connect postgres

I am new to docker and knex. I am connecting docker postgresql with knex connection string. But I am getting below error. I am changing db env variables a lot. But still I am getting the same error. Where I am missing.
knexfile
module.exports = {
client: "pg",
connection: "postgresql://postgres:postgres#localhost:5432/apps",
migrations: {
directory: "./migrations",
tableName: "knex_migraation_apps"
},
}
docker-compose.yml:
version: "3.9"
volumes:
apps-data:
services:
apps:
build: .
depends_on:
- postgres
ports:
- "3001:3001"
environment:
- "APPS_PORT=3001"
postgres:
image: postgres:14.1-alpine
environment:
- "POSTGRES_DB=apps"
ports:
- "5432:5432"
volumes:
- apps-data:/var/lib/pgsql/data
pgadmin-compose:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "placeholder#example.com"
PGADMIN_DEFAULT_PASSWORD: "fakepassword123!"
ports:
- "16543:80"
depends_on:
- postgres

Password auth failed for user "postgres"

I am trying to run my postgres server and nestjs project with docker script and it does fire up the server and database. While firing up it does run migrations too but when I open pgAdmin I see no database there and if i try to create new server i get fatal pasword incorrect error. Also my server crashes too with error saying Password authentication failed for user "postgres". It was running fine yesterday but today its not running at all. I tried pruning everything and made fresh build and then compose up but nothing. Here is docker script
version: "3.5"
services:
dev-api:
container_name: xxxxxxxx-api
build:
context: .
dockerfile: Dockerfile
depends_on:
- dev-db
environment:
DATABASE_URL: postgresql://postgres:postgres#dev-db:5432/xxxxxxx_api
APP_ENV: development
PORT: 3030
WAIT_HOSTS: dev-db:5432
ports:
- "3030:3030"
- "9229:9229"
volumes:
- .:/usr/api/
dev-db:
container_name: xxxxxxxx-postgres
image: postgres:13.5-alpine
restart: always
ports:
- "5432:5432"
volumes:
- ./pg-data:/var/lib/postgresql/data
- ./src/db/docker/init.sql:/docker-entrypoint-initdb.d/dbinit.sql
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=xxxxxxx_api
expose:
- "5432"
pgadmin:
container_name: xxxxxxx-pgadmin
image: dpage/pgadmin4:6.2
ports:
- 8080:80
volumes:
- pgadmin-data:/var/lib/pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=user#postgres.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=80
depends_on:
- dev-db
volumes:
pgadmin-data:

docker-compose.yml for Postgres works either way, but not the expected one

I have the following docker-compose.yml for running Postgres with Docker:
version: '3.8'
services:
postgres:
image: postgres:14.2-alpine
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB: mydatabasename
PGDATA: /data/mydatabasename
volumes:
- postgres:/data/postgres
ports:
- '5432:5432'
networks:
- postgres
restart: unless-stopped
pgadmin:
... placeholder
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
It works. What I don't understand, is that these two combinations work:
PGDATA: /data/mydatabasename
volumes:
- postgres:/data/postgres
and
PGDATA: /data/postgres
volumes:
- postgres:/data/mydatabasename
But this does not work:
PGDATA: /data/mydatabasename
volumes:
- postgres:/data/mydatabasename
I would just get: error: database "mydatabasename" does not exist.
The latter was my first attempt connecting everything though. So I am wondering, why do both fields not map to the actual database name? Thanks for your help!
Docker Compose File
version: '3'
services:
postgres:
image: postgres:alpine
container_name: postgres
restart: always
posts:
- 5432:5432
volumes:
- ./data_backup:/var/lib/postgresql/data #mount the data locally
env_file:
- .env
networks:
- postgres-net
pgadmin:
image: dpage/pgadmin4:6
restart: always
ports:
- 8080:80
volumes:
- pgadmin:/var/lib/pgadmin
env_file:
- .env
depends_on:
- postgres
netwroks:
- postgres-net
networks:
postgres-net:
name: postgres-net
.env File
#postgres
POSTGRES_PASSWORD="secure_password"
POSTGRES_DB=your_db_name
POSTGRES_USER=db_user_name
#pgadmin
PGADMIN_DEFAULT_EMAIL=ariful#firora.com
PGADMIN_DEFAULT_PASSWORD="secure_password"
PGADMIN_LISTEN_PORT=80
Since you may try to bind the database folder which was never created. Alternatively you can bind /var/lib/postgresql/data this path to store whatever database you created or about create in future.

How to fix "Cannot start service pgsql: b'OCI runtime create failed:" when trying to up a pgsql container?

I get the following
ERROR: for marx_pgsql_1 Cannot start service pgsql: b'OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/docker-entrypoint.sh\": permission denied": unknown'
when i try to fire up a pgsql alpine docker image.
Here is my docker-compose.yml
web:
image: nginx:1.17.1-alpine
ports:
- "80:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
build: .
volumes:
- ./code:/code
links:
- pgsql
pgsql:
image: yobasystems/alpine-postgres:latest
environment:
POSTGRES_DB: bookmarx
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
expose:
- "5432"
volumes:
- ./data:/var/lib/postgresql/data
restart: always
How do we fix this ?
I think the script docker-entrypoint.sh is not executable , I suggest to do the following:
create a Dockerfile:
FROM yobasystems/alpine-postgres:latest
RUN chmod +x docker-entrypoint.sh
update your docker-compose:
pgsql:
build: .
environment:
POSTGRES_DB: bookmarx
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
expose:
- "5432"
volumes:
- ./data:/var/lib/postgresql/data
restart: always

I cannot connect from adminer to postgresql

I try to up postgresql and adminer via docker container. But from adminer I cannot enter to postgresql with password and user I whote.
SQLSTATE[08006] [7] FATAL: password authentication failed for user "root"
I tried all.
version: '3'
services:
web:
build: .
environment:
- APACHE_RUN_USER=www-data
volumes:
- ./blog:/var/www/html/
ports:
- 8080:80
working_dir: /var/www/html/
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: kisphp
POSTGRES_USER: root
POSTGRES_DB: kisphp
ports:
- "5432:5432"
volumes:
- ./postgres:/var/lib/postgresql/data
adminer:
image: adminer
restart: always
ports:
- "6080:8080"
This docker-compose configuration works well.
Try recreating it from scratch:
Delete ./postgres folder
docker-compose stop
docker-compose down
docker-compose up -d