Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Please help me solve this issue

When I build the images and do docker-compose up postgres throws and psql error.

Here I am trying to create a new user and a new database. And after that I psql into the new database and create two extensions.

All of these steps are run by a single bash script.

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 file

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 "$@"

After building the image when I docker-compose up it throws an error

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
600 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...