Skip to content

Check exit statuses of scripts in /docker-entrypoint-initdb.d #112

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

Closed
orzeh opened this issue Jan 8, 2016 · 6 comments · Fixed by #118
Closed

Check exit statuses of scripts in /docker-entrypoint-initdb.d #112

orzeh opened this issue Jan 8, 2016 · 6 comments · Fixed by #118

Comments

@orzeh
Copy link
Contributor

orzeh commented Jan 8, 2016

I would be nice to have detected non-zero statuses of scripts and executed sql files in docker-entrypoint.sh.
First non-zero exit status should stop container setup avoiding possibly inconsistent database state.

WDYT?

Eventually I can make PR with this.

@yosifkit
Copy link
Member

yosifkit commented Jan 8, 2016

As far as the shell scripts, those will automatically fail on any line with a simple command that has a non-zero status, since they inherit the set -e of the entrypoint, because they are sourced to give them access to the local variables, rather than run in another shell. For sql scripts, it looks like it might fail to fail with the &&, since that makes it no longer a "simple command" (see set -e docs).

@orzeh
Copy link
Contributor Author

orzeh commented Jan 18, 2016

Oh, I've missed the set -e at the beginning.
So to break on SQL script error the for loop can be just slightly modified, as below:

for f in /docker-entrypoint-initdb.d/*; do
    case "$f" in
        *.sh)  echo "$0: running $f"; . "$f" ;;
        *.sql) 
            echo "$0: running $f" 
            psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f"
            echo 
            ;;
        *)     echo "$0: ignoring $f" ;;
    esac
    echo
done

Then -v ON_ERROR_STOP=1 is needed to psql return non-zero exit code (see docs).

@yosifkit
Copy link
Member

👍 Seems solid to me! Thanks for working on this. Please make sure to follow the indentation of the file, which is tabs 😉.

@orzeh
Copy link
Contributor Author

orzeh commented Jan 18, 2016

Ok, I`ll make PR tomorrow.

orzeh pushed a commit to orzeh/postgres that referenced this issue Jan 19, 2016
@syntropo
Copy link

syntropo commented Nov 1, 2016

Any way to make this new behavior optional? It was actually helpful in my case to ignore errors encountered when running sql files on startup.

@orzeh
Copy link
Contributor Author

orzeh commented Nov 2, 2016

Of course it can be parametrized by introducing for example IGNORE_SQL_ERRORS flag or so.

In your particular case you can run SQL scripts from *.sh files like shown in the docs:

For example, to add an additional user and database, add the following to /docker-entrypoint-initdb.d/init-user-db.sh:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
   CREATE USER docker;
   CREATE DATABASE docker;
   GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

but remove the -v ON_ERROR_STOP=1 switch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants