why is PGDATA directory empty? - postgresql

I created a docker image with the following docker-compose.yml file
version: "3.7"
services:
db:
image: postgres:alpine
container_name: db
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_INITDB_ARGS: '-A md5'
volumes:
- ./pgdata:/var/lib/postgressql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "psql", "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}#db/${POSTGRES_DB}"]
interval: 30s
timeout: 5s
retries: 5
api:
build: api
container_name: api
volumes:
- ./api/migrations:/migrations
ports:
- "8080:8080"
links:
- db
depends_on:
db:
condition: service_healthy
when I do docker-compose up everything is working fine. I am able to connect to Postgres, I can create tables. I can query those tables.
The only problem is that the ./pgdata directory is empty! why is that? since I have done volumes: - ./pgdata:/var/lib/postgressql/data I should have some files getting created in this directory as I create databases and tables right?
I did ls -al command in the pgdata directory and it shows nothing.

I entered the docker-hub-psql-site and checked the working directory.
https://hub.docker.com/_/postgres
In the case of postgresql, it was set as the PGDATA environment variable, and it was as follows.
Dockerfile
...
ENV PGDATA=/var/lib/postgresql/data
...
In other words, I have confirmed that there is a typo postgressql in your docker-compose.yaml, and it will be fixed if you modify it as follows.
version: "3.7"
services:
db:
image: postgres:alpine
container_name: db
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_INITDB_ARGS: '-A md5'
volumes:
# - ./pgdata:/var/lib/postgressql/data
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "psql", "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}#db/${POSTGRES_DB}"]
interval: 30s
timeout: 5s
retries: 5
[NOTE]
Apart from the question, I know that the healthcheck option in docker-compose only supports version 2.
Please refer to the article below stackoverflow/docker-compose-healthcheck-does-not-work

Related

I don't manage to configure volumes in docker-compose to have a folder reachable by dockerized pgAdmin, to restore a database

I have setup from a tutorial a pgadmin4 container and a PostgreSQL container with the following docker-compose:
version: "2"
services:
db:
image: postgres:14
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
volumes:
- db-data:/opt/docker/pgadmin4/postgresql/data
- /home/laurent/Documents/Informatique/Docker/sample-database:/opt
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#linuxhint.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
- /home/laurent/Documents/Informatique/Docker/sample-database:/opt
links:
- "db:pgsql-server"
volumes:
db-data:
pgadmin-data:
In both conainers, I have mounted the path /home/laurent/Documents/Informatique/Docker/sample-database where I have put a sample database.
After having created the dvdrental database as explained in the tutorial, I try to import it with restore, but I cannot find my /opt/dvdrental.tar as expected.
When I Select file, I get this:
If I add opt in the path and refresh, nothing more.
But if I open a console in both containers, I have /opt/dvdrental.tar.
What do I miss?

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.

Does Postgres Store the POSTGRES_USER and POSTGRES_PASSWORD When the Data Directory is Created

Experimenting with Postgres and Pgadmin4 with Docker Compose. Initially I use this as my docker-compose.yml file.
version: '3.8'
services:
db:
container_name: pg_container
image: postgres
restart: always
volumes:
- ./postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
volumes:
- ./data/pgadmin:/var/lib/pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
links:
- "db:psql-server"
I ran:
chown -R 5050:5050 data
So that pgadmin4 could save it's configuration and it worked beautifully. Then I tried to bring it down and update the POSTGRES_PASSWORD to test.
docker-compose down
UPDATED docker-compose.yml file to:
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: test
And then brought it back up:
docker-compose up
To my surprise when connecting to Postgres from Pgadmin4 test does not work as the password, but root does. So my question is when Postgres creates the data directory does it store the credentials somewhere in there? Is there a way to update the password and user once it has been initially created?

How do I run SQL Scripts after DB Initialized from Docker-Compose?

I have the Docker Compose file below. I'm trying to run the following:
Set up Postgres
Run Entity Framework to set up my schemas/tables
Set up PG Admin
Run some SQL scripts on the database.
The I can get the first three items done no problem, but I'm not sure where to put the running of my SQL scripts. Right now it's on the last line of the YAML, but I'm sure this is wrong. Where would I put this? I'm not sure how to reference the database I'd set up earlier to run the SQL on.
version: '3.8'
services:
#SET UP POSTGRES
db:
image: postgres
restart: always
environment:
POSTGRES_USER: marmalade
POSTGRES_PASSWORD: marmalade
POSTGRES_DB: marmalade
ports:
- "15432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U marmalade"]
interval: 5s
timeout: 5s
retries: 5
#RUN ENTITY FRAMEWORK TO INITIALIZE DATABASE
db-migrator:
image: ${DOCKER_REGISTRY-}db-migrator
build:
context: ../../../
dockerfile: src/marmalade/Dockerfile
environment:
- DOTNET_ENVIRONMENT=IntegrationTest
depends_on:
db:
condition: service_healthy
#SET UP PGADMIN
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: marmalade
ports:
- "5050:80"
volumes:
- ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
- ./sql/admin_schema.sql:/docker-entrypoint-initdb.d/admin_schema.sql #<- WHERE DO I PUT THIS?
Its correct but it needs to be in your Db service
Example:
services:
my_db:
image: postgres:latest
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
UPDATE:
problem with running in any other service is that its not going to have the credentials to connect to the database. So you can just create a shell script and run it the old fashioned way like so:
services:
some_service:
image: your_image
volume: ./init.sh:/init.sh
entrypoint: sh -c "/init.sh"
assuming of course that you have the shell already installed in your image

Cant login with docker compose to Postgres via pgadmin

I have the following docker-compose:
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: postgres
volumes:
- postgres:/pgdata
ports:
- "5432:5432"
pdadmin:
image: dpage/pgadmin4
restart: always
ports:
- "8080:80"
environment:
PGADMIN_DEFAULT_EMAIL: example#gmail.com
PGADMIN_DEFAULT_PASSWORD: example
volumes:
postgres:
when I try to login to postgres via pgadmin i am getting the error: password authentication failed for user "postgres"
I am trying with user postgres and password postgres.
I also tryied to add explisit POSTGRES_USER to the env with no success.
How can I login to my postgres database?
Thanks
you're very close:
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_USER: postgres # <-- add this
POSTGRES_PASSWORD: postgres
volumes:
- postgres:/pgdata
ports:
- "5432:5432"
pdadmin:
image: dpage/pgadmin4
restart: always
ports:
- "8080:80"
environment:
PGADMIN_DEFAULT_EMAIL: example#gmail.com
PGADMIN_DEFAULT_PASSWORD: example
depends_on: # <-- add this
- db # <-- add this
links: # <-- add this
- db # <-- add this
volumes:
postgres:
Then connect to pgadmin http://localhost:8080
when you want to add new server you need to add the IP of the container
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>
That it !
try it
PGADMIN_DEFAULT_PASSWORD: Example123
Use a mixture of upper and lower case letters and numerics