Postgres Docker psql 错误:角色“postgres”不存在

时间:2021-01-11 16:49:24

标签: bash postgresql docker docker-compose

请帮我解决这个问题

当我构建图像并执行 docker-compose up postgres throws 和 psql 错误时。

在这里我尝试创建一个新用户和一个新数据库。之后我 psql 进入新数据库并创建两个扩展。

所有这些步骤都由一个 bash 脚本运行。

Dockerfile - db

FROM postgres:12

ENV PG_MAJOR 12

LABEL maintainer="PostGIS Project - https://postgis.net"

ENV POSTGIS_MAJOR 2.5
ENV POSTGIS_VERSION 2.5.5+dfsg-1.pgdg100+2

RUN apt-get update \
      && apt-cache showpkg postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR \
      && apt-get install -y --no-install-recommends \
           postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION \
           postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR-scripts=$POSTGIS_VERSION \
      && rm -rf /var/lib/apt/lists/*

RUN mkdir /app

COPY ./docker/db/.pgpass /app

RUN chmod 600 /app/.pgpass

RUN mkdir -p /docker-entrypoint-initdb.d

COPY ./docker/db/initdb-postgis.sh /docker-entrypoint-initdb.d/10_postgis.sh
COPY ./docker/db/update-postgis.sh /usr/local/bin

env 文件

DB_USER=testuser
DB_USERNAME=postgres
DB_PASSWORD=testpass1223
DB_NAME=testdb

bash script -db

#!/bin/bash
# Immediately exits if any error occurs during the script
# execution. If not set, an error could occur and the
# script would continue its execution.
set -o errexit
# Creating an array that defines the environment variables
# that must be set. This can be consumed later via arrray
# variable expansion ${REQUIRED_ENV_VARS[@]}.
readonly REQUIRED_ENV_VARS=(
  "DB_USER"
  "DB_USERNAME"
  "DB_PASSWORD"
  "DB_NAME")
# Main execution:
# - verifies if all environment variables are set
# - runs the SQL code to create user and database
main() {
  check_env_vars_set
  init_user_and_db
  create_extension
}
# Checks if all of the required environment
# variables are set. If one of them isn't,
# echoes a text explaining which one isn't
# and the name of the ones that need to be
check_env_vars_set() {
  for required_env_var in ${REQUIRED_ENV_VARS[@]}; do
    if [[ -z "${!required_env_var}" ]]; then
      echo "Error:
    Environment variable '$required_env_var' not set.
    Make sure you have the following environment variables set:
      ${REQUIRED_ENV_VARS[@]}
Aborting."
      exit 1
    fi
  done
}

# Perform all actions as $POSTGRES_USER
export PGUSER="$DB_USERNAME"

# Performs the initialization in the already-started PostgreSQL
# using the preconfigured POSTGRE_USER user.
init_user_and_db() {
  echo "-----------------------------------------------"
  psql -v ON_ERROR_STOP=1 --username "$DB_USERNAME" <<-EOSQL
    CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
    CREATE DATABASE $DB_NAME;
    GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOSQL
}

create_extension() {
  echo "-----------------------------------------------"
  PGPASSFILE=/app/.pgpass 
  psql -h localhost -U "$DB_USER" -d "$DB_NAME" <<-EOSQL
    CREATE EXTENSION IF NOT EXISTS postgis;
    CREATE EXTENSION IF NOT EXISTS pg_trgm;
EOSQL
}
# Executes the main routine with environment variables
# passed through the command line. We don't use them in
# this script but now you know ?
main "$@"

当我 docker-compose up 构建图像后,它会引发错误

Traceback

db_1         | CREATE DATABASE
db_1         | 
db_1         | 
db_1         | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/10_postgis.sh
db_1         | -----------------------------------------------
db_1         | 2021-01-11 16:38:33.493 UTC [72] FATAL:  role "postgres" does not exist
db_1         | psql: error: FATAL:  role "postgres" does not exist
thetruck_db_1 exited with code 2

0 个答案:

没有答案