1

My problem:

When importing the database it seems (now) that the database is dropped and can then not be connected. (That did always work before. Import script seems not to be changed).

The whole thing happens within a docker container. Which on my first guess is not the problem.

Creation of the SQL used to import:

pg_dump --host=$HOSTNAME --create --clean --format=plain --username=$USERNAME --file=$DIR_BACKUPS/$DATABASE-latest.sql $DATABASE

(The $VARIABLES are filled from the outside.)

What happens:

The exported SQL I pass to docker for importing:

cat mydbname-latest.sql | docker exec -i test-postgres-9.6 psql -U postgres

When the script runs it seems that the database is being dropped but cannot be connected to it later. I guess the database creation failed. It did exist and was connectable on the beginning before running the dumb.

SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
DROP DATABASE
ERROR:  invalid locale name: "de_DE.UTF-8"
ERROR:  database "mydbname" does not exist
\connect: FATAL:  database "mydbname" does not exist

Why isn't there a script failure on database creation failure, which is invisible?
Why does the ALTER DATABASE statement run through (if the database was not created)?

Here is the beginning of created SQL from the export.

--
-- PostgreSQL database dump
--

-- Dumped from database version 9.6.10
-- Dumped by pg_dump version 9.6.10

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

DROP DATABASE mydbname;
--
-- Name: mydbname; Type: DATABASE; Schema: -; Owner: postgres
--

CREATE DATABASE mydbname WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'de_DE.UTF-8' LC_CTYPE = 'de_DE.UTF-8';

ALTER DATABASE mydbname OWNER TO postgres;

\connect mydbname

...

the \connect mydbname line is subject to throw the error.

If anyone needs more informations let me know. I appreciate your help.

Script for creating docker container from image:

docker run -d --name $DOCKER_CONTAINER_NAME -e POSTGRES_DB=$POSTGRES_DATABASE_NAME -e POSTGRES_USER=$POSTGRES_USER -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -p $POSTGRES_PORT:5432 -v $POSTGRES_DATA:/var/lib/postgresql/data $DOCKER_IMAGE

with these variables set:

DOCKER_CONTAINER_NAME=test-postgres-9.6
DOCKER_IMAGE=test/postgres:9.6
POSTGRES_DATABASE_NAME=mydbname
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_PORT=5402
POSTGRES_DATA=/Users/Itsme/docker/test/psql-data
Dirk Schumacher
  • 1,479
  • 19
  • 37

1 Answers1

2

Docker deals with the problem indeed! Wrong guess on my side.
The logfile actually does show it by the log output:
ERROR: invalid locale name: "de_DE.UTF-8"

The OS started within Docker is not set up to deal with my desired language.

I am having a a german setup as SQL dumb. This I need to store onto an OS and Database which is configured (somewhat) with the same language settings.

I am using the Dockerfile https://github.com/appropriate/docker-postgis/blob/master/9.6-2.4/Dockerfile (7b8355b on 22 Jun 2018) by Mike Dillon (thanks to him by the way :-).

I did adjust the Dockerfile to get it working:

# Taken from: https://github.com/appropriate/docker-postgis/blob/master/9.6-2.4/Dockerfile (commit 22-JUN-2018 7b8355b985846c1591f596eae0cae78689a36a25)
# Adjusted locales

FROM postgres:9.6
MAINTAINER Mike Dillon <mike@appropriate.io>

ENV POSTGIS_MAJOR 2.4
ENV POSTGIS_VERSION 2.4.4+dfsg-4.pgdg90+1

# manually inserted (idea taken from http://jaredmarkell.com/docker-and-locales/)

RUN apt-get update \
      && apt-get install -y locales locales-all \
      && 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 \
           postgis=$POSTGIS_VERSION \
      && rm -rf /var/lib/apt/lists/*

ENV LC_ALL de_DE.UTF-8
ENV LANG de_DE.UTF-8
ENV LANGUAGE de_DE.UTF-8

RUN mkdir -p /docker-entrypoint-initdb.d
COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/postgis.sh
COPY ./update-postgis.sh /usr/local/bin

I did adjust the first RUN command by installing locales and then switched the environment to my needed language.

By the way, the last command fails since it does not find the update-postgis.sh file.

What helped me was http://jaredmarkell.com/docker-and-locales/ getting an idea about the problem and then finally this comment https://stackoverflow.com/a/41797247/845117 which solved it for me.

Dirk Schumacher
  • 1,479
  • 19
  • 37