The postgres container cannot start in docker

Hello,

I am new to the docker. I tried to start the postgres container in docker but kept receiving below error message:

PostgreSQL Database directory appears to contain a database; Skipping initialization

FATAL: could not open directory "pg_tblspc": No such file or directory

LOG: database system is shut down

The postgres part in my docker-compose.yml is as below:

postgres:
    image: postgres:9.6
    environment:
        - POSTGRES_USER=airflow
        - POSTGRES_PASSWORD=airflow
        - POSTGRES_DB=airflow
    # Uncomment these lines to persist data on the local filesystem.
        - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
        - ./pgdata:/var/lib/postgresql/data/pgdata

I searched /var/lib on my machine and there is no postgresql folder existing. No sure if it was to do with the user permission.

Any suggestions would be appreciated.

You don’t tell us what system you use, so it’s not quit easy to help you.
Try to replace the mounted folder from your host with a named volume ‘pgdata’ and mount it into ...postgresql/data, not ...postgresql/data/pgdata:

services:
  postgres:
    ...
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

You’ll see the warning ‘PostgreSQL Database directory appears to contain a database; Skipping initialization’ on every subsequent start, this is nothing to worry about, but the ‘could not open…’ error probably indicates that the permissions of your mounted folder are incorrect.

This folder only exists in the container, not on your host.