Coder Social home page Coder Social logo

Comments (21)

wyuenho avatar wyuenho commented on August 28, 2024 11

I just ran into this problem. I feel the initdb scripts will only run when the volume is first created should be made clear in the image description here.

from postgres.

jmls avatar jmls commented on August 28, 2024 7

I hit this problem - the issue is that the script is only run if there is no postgres data directory ;)

if [ -z "$(ls -A "$PGDATA")" ]; then

I think it would be better to have the script run a "initdb" function for all brand spanking new installations, and a "always run" function

hth

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024 4

@danwyry, my newest idea is that the volume was persisting. With docker compose it tries really hard to keep volumes (ie your initialized database files). Most of the entrypoint script only runs on first initialization. docker-compose rm -v should get rid of all the containers and their volumes if you want to start fresh on the database.

from postgres.

brendan-rius avatar brendan-rius commented on August 28, 2024 2

Any ETA on when we would be able to easily re-launch the init script (via a command maybe?), without removing the volumes?

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024

Random debug thought: is the script executable in the container, maybe also ownership?

from postgres.

kevinburke avatar kevinburke commented on August 28, 2024

These are both possibilities, though if they were not owned/executable by the right process, it seems from this script that Docker would bail out with a non-zero exit code.

https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh#L48

from postgres.

md5 avatar md5 commented on August 28, 2024

from postgres.

Cactusbone avatar Cactusbone commented on August 28, 2024

sometime the scripts could be used to perform update operations on the existing data.
for example updating an extension on all affected dbs.
It would be nice to have a base install scripts folder and an update scripts one

from postgres.

jayfk avatar jayfk commented on August 28, 2024

I hit this problem - the issue is that the script is only run if there is no postgres data directory ;)

if [ -z "$(ls -A "$PGDATA")" ]; then
I think it would be better to have the script run a "initdb" function for all brand spanking new installations, and a "always run" function

I agree. The current approach makes it extremely hard to supply your own postgresql.conf with a Dockerfile that uses the official postgres image as base.

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024

@jayfk, #72 should fix that part.

from postgres.

danwyryunq avatar danwyryunq commented on August 28, 2024

I too met this problem and have been trying to debug it for a week already.
my .sh scripts in docker-entrypoint-init.d/ never run.
I tryed extending Dockerfile from 9.4 image, and also I tryed building it myself from a copy of the original Dockerfile and docker-entrypoint.sh. It seems to be that the docker-entrypoint.sh never gets into the
if [ "$1" = 'postgres' ]; then
condition on line 17 of the script.
I ran out of ideas. Any help on this ?

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024

@danwyry, what are you running? Any information you can give to help us reproduce the issue?

from postgres.

danwyryunq avatar danwyryunq commented on August 28, 2024

Sure, this is what I tryied so far:
I downloaded 9.4's Dockerfile and docker-entrypoint.sh to a directory on my computer
I added the functions detailed below to docker-entrypoint.sh and called them right after the set_listen_address '*' function is called.
on my docker-compose.yml I put :

build: my_postgres 
  environment:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
  volumes_from:
   - data
  ports:
   - "5434:5432"

being 'my_postgres' the directory name under which y located the Dockerfile and entrypoint.

I ran docker-compose build.
It ran with no errors, but postgresql.conf.

I performed several hardcoded tests to see where the docker-entrypoint.sh exits and realized itΕ› not even getting past the follwing condition (which in the original file is in line 17)
if [ "$1" = 'postgres' ]; then

These are the functions I added that configure some settings on postgresql.conf (I already tested them on their own and they work ok). :

set_bytea_output() {
    sed -ri '/^[# ]*[# ]*[# ]*bytea_output[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf" 
   echo "bytea_output = '$1'" >> "$PGDATA/postgresql.conf" 

}
set_standard_conforming_strings() {
  sed -ri '/^[# ]*[# ]*[# ]*standard_conforming_strings[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf" 
  echo "standard_conforming_strings = $1" >> "$PGDATA/postgresql.conf" 
}

BTW, I also tryed puting these functions on an alternate script inside docker-entrypoint-init.d and creating a Dockerfile extending from the official image:

FROM postgres:9.4
COPY docker-entrypoint-initdb.d/settings_postgres_conf.sh /docker-entrypoint-initdb.d/settings_postgres_conf.sh
RUN chmod 0755 /docker-entrypoint-initdb.d/settings_postgres_conf.sh

ENV PGDATA /var/lib/postgresql/data
VOLUME /var/lib/postgresql/data

But it won't work either.

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024

Hmmm... I am unable to reproduce. I started with the docker-entrypoint.sh and added your functions with some output to see it works. I guessed on the values to apply.

diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh
index 87d7e3b..3334092 100755
--- a/docker-entrypoint.sh
+++ b/docker-entrypoint.sh
@@ -6,6 +6,16 @@ set_listen_addresses() {
        sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
 }

+set_bytea_output() {
+       sed -ri '/^[# ]*[# ]*[# ]*bytea_output[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf"
+       echo "bytea_output = '$1'" >> "$PGDATA/postgresql.conf"
+}
+set_standard_conforming_strings() {
+       sed -ri '/^[# ]*[# ]*[# ]*standard_conforming_strings[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf"
+       echo "standard_conforming_strings = $1" >> "$PGDATA/postgresql.conf"
+}
+
 if [ "$1" = 'postgres' ]; then
        mkdir -p "$PGDATA"
        chown -R postgres "$PGDATA"
@@ -84,6 +94,13 @@ if [ "$1" = 'postgres' ]; then

                gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
                set_listen_addresses '*'
+               set_bytea_output 'hex'
+               set_standard_conforming_strings 'off'
+               echo '*****************************'
+               echo '*****************************'
+               cat "$PGDATA/postgresql.conf"
+               echo '*****************************'
+               echo '*****************************'

                echo
                echo 'PostgreSQL init process complete; ready for start up.'

Then I ran postgres with the modified entrypoint:

$ docker run -it --rm -v ~/postgres/modified-docker-entrypoint.sh:/docker-entrypoint
.sh:ro -e POSTGRES_PASSWORD=password postgres:9.4

from postgres.

danwyryunq avatar danwyryunq commented on August 28, 2024

would you mind trying it within a docker-compose configuration to see if maybe that's making any difference?

from postgres.

mericano1 avatar mericano1 commented on August 28, 2024

Hi @danwyry did you ever get your scripts running? I am having the same issue using docker-compose

from postgres.

danwyryunq avatar danwyryunq commented on August 28, 2024

nope, couldn't do it. But I stopped trying after a while

from postgres.

blag avatar blag commented on August 28, 2024

I had this exact same issue and it was solved by removing the volume associated with the container in docker-compose.yml.

@brendan-rius If you just need the data saved in the volume (and you don't have anything else stored there), you should be able to run your postgres container (see the "... or via psql" section here) manually, dump your database, remove the volume, recreate everything, and import the database again.

from postgres.

ekimia avatar ekimia commented on August 28, 2024

is there an easy way to just set all listen addresses so my code can access postgres? My code isnt copied into the image in development mode.

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024

@ekimia, what do you mean? If you have any shell scripts in /docker-entrypoint-initdb.d/ then they can access postgres via localhost.

If you are talking about accessing postgres from another container, then your code will have to wait for postgres to be ready, after it has done its initialization and run through any initdb scripts.

from postgres.

yosifkit avatar yosifkit commented on August 28, 2024

I feel like the original issue here has been solved. If you are still having trouble getting /docker-entrypoint-initdb.d/ scripts to run, please open a new issue with as much information as possible.

from postgres.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.