简体   繁体   中英

Docker not recognizing Postgresql data directory

I am desperately trying to get a Docker project I have inherited up and running, and Docker is giving me no end of problems. When trying to start up my containers I get the following error on my Postgresql container:

FATAL:  "/var/lib/postgresql/data" is not a valid data directory
DETAIL:  File "/var/lib/postgresql/data/PG_VERSION" does not contain valid data.
HINT:  You might need to initdb.

The project is a Rails project using Redis, ElasticSearch, and Sidekiq containers as well - those all load fine.

docker-compose.yml:

postgres:
  image: postgres:9.6.2
  environment:
    POSTGRES_USER: $PG_USER
    POSTGRES_PASSWORD: $PG_PASS
  ports:
    - '5432:5432'
  volumes:
    - postgres:/var/lib/postgresql/data

/var/lib/postgresql/data is owned by the postgres user (as it should be I believe) and the postgresql service starts up and runs fine on its own.

I have tried running initdb from the /usr/lib/postgresql/9.6/bin directory, as well as from docker (from docker it doesn't seem to persist or even create anything... if anyone knows why I would be interested in knowing)

The contents of the /var/lib/postgresql/data directory:

drwxrwxrwx 19 postgres postgres  4096 Jun 28 20:41 .
drwxr-xr-x  5 postgres postgres  4096 Jun 28 20:41 ..
drwx------  5 postgres postgres  4096 Jun 28 20:41 base
drwx------  2 postgres postgres  4096 Jun 28 20:41 global
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_clog
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_commit_ts
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_dynshmem
-rw-------  1 postgres postgres  4468 Jun 28 20:41 pg_hba.conf
-rw-------  1 postgres postgres  1636 Jun 28 20:41 pg_ident.conf
drwx------  4 postgres postgres  4096 Jun 28 20:41 pg_logical
drwx------  4 postgres postgres  4096 Jun 28 20:41 pg_multixact
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_notify
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_replslot
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_serial
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_snapshots
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_stat
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_stat_tmp
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_subtrans
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_tblspc
drwx------  2 postgres postgres  4096 Jun 28 20:41 pg_twophase
-rw-------  1 postgres postgres     4 Jun 28 20:41 PG_VERSION
drwx------  3 postgres postgres  4096 Jun 28 20:41 pg_xlog
-rw-------  1 postgres postgres    88 Jun 28 20:41 postgresql.auto.conf
-rw-------  1 postgres postgres 22267 Jun 28 20:41 postgresql.conf  

PG_VERSION contains 9.6

Any help is much appreciated.

you're changing default postgresql data path hence you need to initialize the database. try this

volumes:
    - ./init.sql:/docker-entrypoint-initdb.d/init.sql

here is the init.sql file

CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

So when have postgres:/var/lib/postgresql/data , it's going to mount /var/lib/postgresql/data to a docker data volume called postgres . Docker data volumes are all stored together in a location that varies depending on the OS.

Try changing it to ./postgres to have it create a directory called postgres relative to your working directory.

Since the source is changing it will recreate the database, and I'd be willing to be fix the error your seeing. If not, it could be a permission issue on the host os.

I had the same issue, I restarted Docker daemon and even restarted the machine, but didn't fix the issue.
The path /var/lib/postgresql/data was not even on the FS.

Note: Performing a docker ps was not showing the postgresql container as running.

Solution (docker-compose):

  • Close postgres container:
    docker-compose -f <path_to_my_docker_compose_file.yaml> down postgres

  • Start postgres container:
    docker-compose -f <path_to_my_docker_compose_file.yaml> -d up postgres

-- That did the trick! --

Solution (docker):

docker stop <postgres_container>
docker start <postgres_container>

Another solution that you can try:
initdb <temporary_volum_folder>
Example:
initdb /tmp/postgres
docker-compose -f <path_to_my_docker_compose_file.yaml> -d up postgres
or
initdb /tmp/postgres docker start <postgres_container>

Note:
In my case the postgres image is defined in docker-compose.yaml file, and it can be observed I don't define PG_DAT nor PG_VERSION and the container runs ok.

  postgres:
    image: postgres:9.6.14
    container_name: postgres
    environment:
      POSTGRES_USER: 'pg12345678'
      POSTGRES_PASSWORD: 'pg12345678'
    ports:
      - "5432:5432"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM