After using docker-compose down,i lost my data - postgresql

First,i changed pg_hba.conf,changed "host all all all scram-sha-256" to "host all all all md5". after that,i executed "docker-compose down and docker-compose up -d",but not executed "docker-compose down -v".
After docker starts successfully,I found that the data is lost.i executed "docker volume ls",the result is empty.what caused this phenomenon?
Could someone possibly give me some pointers?Thank you very much!
This is my docker-compose.yml
db:
image: postgres
container_name: postgres
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: polardb
POSTGRES_PASSWORD: 123456
ports:
- 5432:5432
volumes:
- /opt/deploy/postgres/data:/var/lib/postgresql/data

What was lost was the data of the test server, and the data should not be recovered, but I wanted to understand what caused it, so I found a new server for testing, and found that the data was not lost after the same steps.

Related

Prisma/ User `postgres` was denied access on the database `practice.public`

I'm quite new to Prisma, so if I overlook something basic, please forgive me.
I was running postgreSQL DB on a docker container and tried npx prisma migrate dev from local.
However, the following error occurred:
Error: P1010: User `postgres` was denied access on the database `practice.public`
my docker-compose.yml file and .env file were the followings:
// docker-compose.yml
version: "3"
services:
postgres:
image: postgres:14.1
container_name: postgres
hostname: postgres
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: practice
volumes:
- ./db:/var/lib/postgresql/data
restart: unless-stopped
// .env
DATABASE_URL="postgresql://postgres:postgres#localhost:5432/practice?schema=public"
I tried everything I could come up with, and accidentally found a solution.
First, I checked the username of my host computer.
echo $USER
>myusername
Then put the username into the DATABASE_URL in .env file, instead of the POSTGRES_USER I defined in docker-compose.yml.
// .env
DATABASE_URL="postgresql://myusername:postgres#localhost:5432/practice?schema=public"
npx prisma dev migrate worked for some reason. Congrats.
But I haven't the least idea of why it worked……
If you shed some lights on it, I would really appreciate it.

Docker-compose depends on not waiting until depended on service isn't fully started

version: '3'
services:
server:
container_name: hotel-server
build:
dockerfile: Dockerfiles/server/Dockerfile
context: .
environment:
dbhost: db
links:
- db
depends_on:
- db
restart: always
ports:
- "3456:3456"
db:
image: "custom-postgis:latest"
container_name: hotelsdb
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: alliswell
# POSTGRES_DB: hotels
ports:
- "5437:5432"
volumes:
- hoteldata:/var/lib/postgresql/data #persistence
volumes:
hoteldata: {}
The way I've set up the custom-postgis, its a custom postgres database container that has some initialization scripts which I have to wait until it starts.
The problem is the server service starts before the db service fully starts.
Is there any workaround to that?
Is there any workaround to that?
Yes.
First, realize that depends-on is almost entirely useless. Docker doesn't know anything about your application; it has no way to tell that your database server isn't actually ready to service requests.
The correct solution is to code your application so that (a) it will retry the initial database connection until it is ready, and (b) it will reconnect to the database if the connection should fail. (a) solves the problem you're asking about, and (b) allows you to restart the database container independent of the application container.
If you don't control the code in your application container, you can wrap your main command with a shell script that does something like:
while ! psql -c 'select 1'; do
sleep 1
done
(Setting appropriate authentication options or setting up a .pgpass file)

PostgreSQL Container in Docker Not Authorizing a Correct Password

I have arranged a node.js back end to connect to a redis cache and psql database.
The app I have created is running but I would like to do some database admin and have attempted to log in using pgAdmin - however, my details were rejected.
I thought it might be a pgAdmin thing so I attempted to use the login URI in powershell but again it was rejected.
I checked that the psql service is running on the exposed port (in case I messed up the docker-compose config) and it is...not sure where to go from here.
My docker-compose config for the database is:
# PostgreSQL
postgres:
container_name: postgres
build: ./postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/myapp
POSTGRES_DB: myapp
POSTGRES_HOST: postgres
ports:
- "5432:5432"
I should note that the database is running - I can log in to my front end and access data, etc...
My login attempt:
psql postgres://admin:password#localhost:5432/myapp
And the response:
psql: FATAL: password authentication failed for user "admin"
I think you docker-compose not formatted well if it's not copy-paste issue as the environment variable, not place properly.
# PostgreSQL
postgres:
image: postgres
container_name: postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/myapp
POSTGRES_DB: myapp
POSTGRES_HOST: postgres
ports:
- "5432:5432"
Or you can try
version: '3.7'
services:
postgresdb:
container_name: postgres
environment:
POSTGRES_DB: appdb
POSTGRES_USER: appdb
POSTGRES_PASSWORD: 123123
image: bitnami/postgresql:latest
ports:
- "5432:5432"
Or better to post you Dockerfile, as I see your building your own Docker image, but better to use the offical image of Postgres like the one I posted above.
Also will suggest debugging on container DB first and verify connectivity on the container localhost, debugging and testing with depended containers like connecting from nodejs first here one lost in the actual problem.
Check if your ENV set properly.
docker exec postgres bash -c "printenv "
or
docker exec postgres bash -c "printenv | grep POSTGRES_"
or
docker exec -it postgres bash -c "psql -U admin myapp"

Swift Vapor 3 + PostgreSQL + Docker-Compose Correct configuration?

Currently building a package to test some devOps configurations with AWS. Building an application with Swift Vapor3, PostgreSQL 11, Docker. Given my github Repo the project builds/tests/runs just fine with vapor build vapor test vapor run given that you have a local installation of postgresql installed with a username: test, password: test
However my api is not connecting to my DB and am worried my configuration is wrong.
version: "3.5"
services:
api:
container_name: vapor_it_container
build:
context: .
dockerfile: web.Dockerfile
image: api:dev
networks:
- vapor-it
environment:
POSTGRES_PASSWORD: 'test'
POSTGRES_DB: 'test'
POSTGRES_USER: 'test'
POSTGRES_HOST: db
POSTGRES_PORT: 5432
ports:
- 8080:8080
volumes:
- .:/app
working_dir: /app
stdin_open: true
tty: true
entrypoint: bash
restart: always
depends_on:
- db
db:
container_name: postgres_container
image: postgres:11.2-alpine
restart: unless-stopped
networks:
- vapor-it
ports:
- 5432:5432
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_HOST: db
POSTGRES_PORT: 5432
PGDATA: /var/lib/postgresql/data
volumes:
- database_data:/var/lib/postgresql/data
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: test#test.com
PGADMIN_DEFAULT_PASSWORD: admin
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- vapor-it
restart: unless-stopped
networks:
vapor-it:
driver: bridge
volumes:
database_data:
pgadmin:
# driver: local
Also while reading the Docker postgres docs I came across this in the "Caveats" section.
If there is no database when postgres starts in a container, then postgres will create the default database for you. While this is the expected behavior of postgres, this means that it will not accept incoming connections during that time. This may cause issues when using automation tools, such as docker-compose, that start several containers simultaneously.postgres dockerhub
I have not made those changes because I am not sure how to go about making that file or how the configuration would look. Has anyone done something like this that has some experience with connecting to Postgresql and using vapor as a back end?
The theory is, a well-behaved container should be able to gracefully handle not having its dependencies running, because despite the best efforts of your container scheduler, containers may come and go. So if your app needs a DB, but at any given moment the DB is unavailable, it should respond rationally. For example, returning a 503 for an HTTP request, or trying again after a delay for a scheduled task.
That’s theory though, and not always applicable. In your situation, maybe you really do just need your Vapor app to wait for Postgres to come available, in which case you could use a wrapper script that polls your DB and only starts your main app after the DB is ready.
See this suggested wrapper script from the Docker docs:
#!/bin/sh
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$#"
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
command: ["./wait-for-postgres.sh", "db", "vapor-app", "run"]

Where does the data of postgresql container save ans, and also where the location of container is?

I'm using the nameko from GitHub https://github.com/nameko/nameko-examples
and I deploy this application on docker. Once I stop the container all the data lost and I also not able to find the location of the container. I want to store each database date on my local computer.
You have to mount storage for the postgres container.
https://github.com/nameko/nameko-examples/blob/master/docker-compose.yml
postgres:
container_name: nameko-example-postgres
image: postgres
ports:
- "5433:5432" # Exposing Postgres on different port for convenience
environment:
POSTGRES_DB: "orders"
POSTGRES_PASSWORD: "password"
POSTGRES_USER: "postgres"
restart: always
you can add columes to the above snippet by
volumes:
- ./database:/var/lib/postgresql