Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't populate database via sh script #603

Closed
alexeyd-itsoft opened this issue Jul 29, 2019 · 5 comments
Closed

Can't populate database via sh script #603

alexeyd-itsoft opened this issue Jul 29, 2019 · 5 comments
Labels
question Usability question, not directly related to an error with the image

Comments

@alexeyd-itsoft
Copy link

Have faced with the issue that our sh script doesn't work anymore. it worked until 9.6.8

As far as I have understood the root of the issue is

# diff -u docker-entrypoint.sh-967 docker-entrypoint.sh-968
--- docker-entrypoint.sh-967  2018-02-17 00:27:18.000000000 +0200
+++ docker-entrypoint.sh-968  2018-05-09 21:01:04.000000000 +0300
@@ -95,7 +95,7 @@
     # does not listen on external TCP/IP and waits until start finishes
     PGUSER="${PGUSER:-postgres}" \
     pg_ctl -D "$PGDATA" \
-      -o "-c listen_addresses='localhost'" \
+      -o "-c listen_addresses=''" \
       -w start

Our script has the following check

check1_result=$(psql --host=localhost --port=5432 --version > /dev/null 2>&1)
check2_result=$(psql --host=localhost --port=5432 -l > /dev/null 2>&1)

if [[ ${check1_result} -eq 0 && ${check2_result} -eq 0 ]]; then
   # create db and users
fi

In the console I get the following error

postgresql    | psql: could not connect to server: Connection refused
postgresql    | 	Is the server running on host "localhost" (127.0.0.1) and accepting
postgresql    | 	TCP/IP connections on port 5432?

I there any workaround? Unfortunately I can't update the script

@wglambert wglambert added the question Usability question, not directly related to an error with the image label Jul 29, 2019
@wglambert
Copy link

If you can't modify it to use a unix socket then perhaps you could continually re-run your script for a duration of time so when the server eventually initializes it will then connect. Or you could try a sleep to run the script after a wait period.

The PR for the rationale behind this change #440

@alexeyd-itsoft
Copy link
Author

actually we have 10 tries in the script but it doesn't work anywhere

postgresql    | server started
postgresql    | CREATE DATABASE
postgresql    | 
postgresql    | 
postgresql    | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01-test.sh
postgresql    | + init
postgresql    | + attempt_number=0
postgresql    | + waitForPostgreSQL
postgresql    | + psql --host=localhost --port=5432 --version
postgresql    | + check1_result=0
postgresql    | + psql --host=localhost --port=5432 -l
postgresql    | + check2_result=2
postgresql    | + [[ 0 -eq 0 ]]
postgresql    | + [[ 2 -eq 0 ]]
postgresql    | + '[' 0 -gt 10 ']'
postgresql    | + attempt_number=1
postgresql    | + return 1
postgresql    | + sleep 10
...
...
...
postgresql    | + attempt_number=11
postgresql    | + return 1
postgresql    | + sleep 10
postgresql    | + echo -n .
postgresql    | + waitForPostgreSQL
postgresql    | .+ psql --host=localhost --port=5432 --version
postgresql    | + check1_result=0
postgresql    | + psql --host=localhost --port=5432 -l
postgresql    | + check2_result=2
postgresql    | + [[ 0 -eq 0 ]]
postgresql    | + [[ 2 -eq 0 ]]
postgresql    | PostgreSQL is not ready
postgresql    | + '[' 11 -gt 10 ']'
postgresql    | + echo 'PostgreSQL is not ready'
postgresql    | + exit 254

I'm using the following bash script

#!/bin/bash -x

waitForPostgreSQL() {
    psql --host=localhost --port=5432 --version > /dev/null 2>&1
    check1_result=$?

    psql --host=localhost --port=5432 -l > /dev/null 2>&1
    check2_result=$?

    if [[ $check1_result -eq 0 ]] && [[ $check2_result -eq 0  ]]; then
        return 0
    else
        if [ $attempt_number -gt 10 ]; then
            echo "PostgreSQL is not ready"
            exit 254
        fi
        attempt_number=$((attempt_number + 1))
        return 1
    fi
}

init(){
    attempt_number=0
    while ! waitForPostgreSQL
    do
            sleep 10
            echo -n '.'
    done

    echo "PostgreSQL is ready."
    # Creating users, schema, etc ..
}

init

@wglambert
Copy link

wglambert commented Jul 30, 2019

So your first check returned an exit 0 because it doesn't require authentication and it will always succeed regardless if a database even exists

postgresql | + check1_result=0

That means it connected successfully, however your script requires two successful connections. And in the second one you get an error with an exit 2

root@42d533e02f21:/# psql --host=localhost --port=5432 -l
psql: FATAL:  role "root" does not exist
root@42d533e02f21:/# echo $?
2

If instead you change that to authenticate as a valid user in the database then it resolves (by default localhost connections using postgres don't need a password)

root@42d533e02f21:/# psql --host=localhost -U postgres --port=5432 -l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access 
privileges   
-----------+----------+----------+------------+------------+----------
. . .

root@42d533e02f21:/# echo $?
0

However you can't use --host=localhost if your script is being run by the docker-entrypoint.sh since the temporary daemon started for these scripts only listens on the unix socket. So you can either drop that and have it default to the socket, or specify it explicitly --host /var/run/postgresql
https://github.com/docker-library/docs/tree/master/postgres#initialization-scripts

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/script.sh
+ init
+ attempt_number=0
+ waitForPostgreSQL
+ psql --host=localhost --port=5432 --version
+ check1_result=0
+ psql -U postgres --host /var/run/postgresql --port=5432 -l
PostgreSQL is ready.
+ check2_result=0
+ [[ 0 -eq 0 ]]
+ [[ 0 -eq 0 ]]
+ return 0
+ echo 'PostgreSQL is ready.'

2019-07-30 17:42:07.108 UTC [43] LOG:  received fast shutdown request
waiting for server to shut down....2019-07-30 17:42:07.146 UTC [43] LOG:  aborting any active transactions
2019-07-30 17:42:07.151 UTC [43] LOG:  background worker "logical replication launcher" (PID 50) exited with exit code 1
2019-07-30 17:42:07.151 UTC [45] LOG:  shutting down
2019-07-30 17:42:07.796 UTC [43] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2019-07-30 17:42:07.885 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-07-30 17:42:07.885 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2019-07-30 17:42:07.977 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-07-30 17:42:08.472 UTC [70] LOG:  database system was shut down at 2019-07-30 17:42:07 UTC
2019-07-30 17:42:08.526 UTC [1] LOG:  database system is ready to accept connections

You might also be interested in the healthcheck examples if that's what you're going for

@alexeyd-itsoft
Copy link
Author

Unfortunately doesn't work for me

postgresql    | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01-test.sh
postgresql    | + init
postgresql    | + attempt_number=0
postgresql    | + waitForPostgreSQL
postgresql    | + psql --host /var/run/postgresql --port=5432 --version
postgresql    | + check1_result=0
postgresql    | + psql --host /var/run/postgresql --port=5432 -l
postgresql    | FATAL:  role "postgres" does not exist
postgresql    | + check2_result=2

probably because I changed default postgresql user via -e POSTGRES_USER=myOwnUserName

@wglambert
Copy link

Yeah you'll want -U myOwnUserName to authenticate with Postgres

Going to close since this isn't an issue with the image
If you have further questions you could also try asking the Docker Community Forums, Docker Community Slack, or Stack Overflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

No branches or pull requests

2 participants