Postgres docker instance does not replicating to the slave - postgresql

I am trying to set up postgres docker instances with a replication function
I have working on building this based on this tutorial:
https://medium.com/swlh/postgresql-replication-with-docker-c6a904becf77
And I came across a repo build on this :
https://github.com/mekeerati/docker-compose-example/tree/main/postgresql-read-replica
The only thing I have changed
Since I have a port running on 5432, have changed the port to the following values
--> 127.0.0.1/32 to 127.0.0.1/33 (I am not sure if this was necessary)
and also the docker-compose.yml values
When I run the application I am expecting to replicate but it does not work.
It seems like everything is works fine, what should I do to provide replication properly
pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/33 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/33 trust
host replication all ::1/128 trust
host replication replicator 0.0.0.0/0 trust
host all all all scram-sha-256
docker-compose.yml
version: '3'
services:
# To start replica mode please use `docker-init.sh`
postgres_master:
container_name: postgres_master
image: postgres:14.5
restart: always
volumes:
- ./data:/var/lib/postgresql/data
- ./data-slave:/var/lib/postgresql/data-slave
- ./init-script/config/postgres.conf:/etc/postgresql/postgresql.conf
- ./init-script/config/pg_hba.conf:/etc/postgresql/pg_hba.conf
- ./init-script:/etc/postgresql/init-script
ports:
- "127.0.0.1:5433:5433"
environment:
- POSTGRES_USER=postgresql
- POSTGRES_PASSWORD=postgresql
postgres_slave:
container_name: postgres_slave
image: postgres:14.5
restart: always
volumes:
- ./data-slave:/var/lib/postgresql/data
- ./config/postgres.conf:/etc/postgresql/postgresql.conf
- ./config/pg_hba.conf:/etc/postgresql/pg_hba.conf
ports:
- "127.0.0.1:5434:5433"
environment:
- POSTGRES_USER=postgresql
- POSTGRES_PASSWORD=postgresql

Related

Trying to connect Strapi server to Docker container running Postgres DB: no pg_hba.conf entry for host "127.0.0.1"

So I have my strapi server, which should connect to the Docker container running my Postgres DB, but I get this error when I try to start the development server for the strapi component of my application.
no pg_hba.conf entry for host "127.0.0.1", user "admin", database "store", no encryption
error: no pg_hba.conf entry for host "127.0.0.1", user "admin", database "store", no encryption
This is what is within my pg_hba.conf file on my local
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all scram-sha-256
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
I also have a docker-compose.yml file which creates the postgres database, so I can connect the strapi server to it.
version: '3'
services:
database:
image: 'postgres'
container_name: nndesign
build:
context: .
target: nndesign
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: nuzhat
POSTGRES_DB: store
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./scripts/postgres-data:/var/lib/postgresql/data
- ./scripts/admin_users.sql:/docker-entrypoint-initdb.d/admin_users.sql
This is what I have as environment variables so the strapi server can connect to the postgres db running in the docker container
export default ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: env('DATABASE_HOST', 'localhost'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'store'),
user: env('DATABASE_USERNAME', 'admin'),
password: env('DATABASE_PASSWORD', 'nuzhat'),
ssl: env.bool('DATABASE_SSL', false),
},
},
});
I'm a bit confused on how to revolve this error. Been trying to Google and also use ChatGPT to find a solution, but to no avail.
Any help would be greatly appreciated.
Thank you.

Have the login page but can't connect in phppgadmin

I'm using docker to turn my app.
When I was using the image 'postgres:10-alpine', I didn't have any problem but after I upgraded to 'postgres:14-alpine', I can still access to the database directly in the postgres container but when I try to access with phppgadmin thorugh "http://localhost:9080" , I have the login page but the user + password don't works (but in postgres 10, no problem).
Do you have any idea how to fix it ?
my docker-compose.yml:
# Postgres BDD
postgres:
container_name: postgres
image: postgres:14-alpine
env_file:
- .env
restart : always
ports:
- "5432:5432"
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASS}
volumes:
- postgres_starter_db:/var/lib/postgresql/data
- ./docker/database_utils:/home/db
# Postgres visualization tool
phppgadmin:
image: dockage/phppgadmin:latest
container_name: phppgadmin
env_file:
- .env
links:
- postgres:postgres
ports:
- "9080:80"
environment:
- PHP_PG_ADMIN_SERVER_DESC=PostgreSQL
- PHP_PG_ADMIN_SERVER_HOST=${DATABASE_HOST}
- PHP_PG_ADMIN_SERVER_PORT=${DATABASE_PORT}
the pg_hba.conf in postgres container
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all all scram-sha-256

How to set your pg_hba.conf file in a Docker container?

Good day!
I have a task to delimit user access to databases
for example, that user_1 would have access only to the database of "oranges" and user_2 only to the database of "watermelons"
I try to do this through pg_hba.conf, but I can still connect to the watermelon database through user_1
MY pg_hba.conf
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the PostgreSQL
# documentation for a complete description of this file. A short
# synopsis follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are
#...
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
# local all all trust
# IPv4 local connections:
# host all all 127.0.0.1/32 trust
# IPv6 local connections:
# host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
# local replication all trust
# host replication all 127.0.0.1/32 trust
# host replication all ::1/128 trust
# host all all all scram-sha-256
host oranges user_1 password
host watermelons user_2 password
MY DOCKERFILE
FROM postgres:14.1
RUN rm -f /var/lib/postgresql/data/pg_hba.conf
COPY ./pg_hba.conf /var/lib/postgresql/data/pg_hba.conf
ADD ./init.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
MY DOCKER-COMPOSE
version: "3.9"
services:
#DATABASE_SERVER
postgres:
build: ./databases/postgres
hostname: postgres
environment:
POSTGRES_PASSWORD: root
PGDATA: /var/lib/postgresql/data
ports:
- 5432:5432
What am I doing wrong? I will be grateful for any comment!

Cannot connect to postgreSQL docker container via postico

I'm trying to use Postico to connect to a docker postgreSQL container on my local machine.
I've tried connecting to 0.0.0.0, localhost, and 127.0.0.1. Each give me the following error:
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
0.0.0.0 gives me a similar, but smaller error:
could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose file:
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.23
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: prisma
migrations: true
postgres:
image: postgres:10.5
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Solution found thanks to Egor! I forgot to specify ports: - "5432:5432" inside my docker-compose file. Rookie mistake ;)
I also had issues using Postico to connect to my Postgres DB in a docker container.
Ultimately, my issue was that I had a local Postgres DB running.
As soon as I disconnected my local Postgres DB, I was able to use Postico to connect to my docker DB. With the host set to localhost, I used the POSTGRES_USER, POSTGRES_PASSWORD, and host port as defined in my docker-compose.yml file.
If postgres version doesn't matter, try to change Postgres image to this one, it works for me
And also make sure that you add ports in docker-compose.yml
postgres:
image: postgres
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
ports:
- "5432: 5432"
volumes:
- postgres:/var/lib/postgresql/data
P.s. just updated answer for readability

Changing a postgres containers server port in Docker Compose

I am trying to deploy a second database container on a remote server using Docker compose. This postgresql server runs on port 5433 as opposed to 5432 as used by the first postgresql container.
When I set up the application I get this error output:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.17.0.2) and accepting
web_1 | TCP/IP connections on port 5433?
and my docker compose file is:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433"
ports:
- "5433"
volumes:
- ./backups:/home/backups
web:
build: .
command: bash -c "sleep 5 && python -u application/manage.py runserver 0.0.0.0:8081"
volumes:
- .:/code
ports:
- "81:8081"
links:
- db
environment:
- PYTHONUNBUFFERED=0
I feel the issue must be the postgresql.conf file on the server instance having set the port to 5432 causing the error when my app tries to connect to it. Is there a simple way of changing the port using a command in the compose file as opposed to messing around with volumes to replace the file?
I am using the official postgresql container for this job.
Some people may wish to actually change the port Postgres is running on, rather than remapping the exposed port to the host using the port directive.
To do so, use command: -p 5433
In the example used for the question:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433" # Publishes 5433 to other containers but NOT to host machine
ports:
- "5433:5433"
volumes:
- ./backups:/home/backups
command: -p 5433
Note that only the host will respect the port directive. Other containers will not.
Assuming postgres is running on port 5432 in the container and you want to expose it on the host on 5433, this ports strophe:
ports:
- "5433:5432"
will expose the server on port 5433 on the host. You can get rid of your existing expose strophe in this scenario.
If you only want to expose the service to other services declared in the compose file (and NOT localhost), just use the expose strophe and point it to the already internally exposed port 5432.