1

I have a simple Dockerfile:

FROM postgres:latest

ENV POSTGRES_PASSWORD password
ENV POSTGRES_USER postgres
ENV POSTGRES_DB evesde


COPY init.sh /docker-entrypoint-initdb.d/

and my init file is chmod' to 777:

#!/bin/bash
psql -U "postgres" -d "evesde" -e "create role yaml with login encrypted password 'password';"

when running a container it will say:

psql: warning: extra command-line argument "create role yaml with login encrypted password 'password';" ignored

I'm not sure why this is happening, and when doing an interactive terminal, this command seemingly worked. I don't see any additional information and wasn't sure what was going wrong.

The Postgres Docker page is: https://hub.docker.com/_/postgres

When looking at it deeper, I was noticing that running the command itself fails in an interactive Terminal with the same error, but the command runs when I am in Postgres: psql -U "postgres" -d "evesde" and run the command, it works.

I think it may be related to passing the command in through the exec command is where it fails. likely related to '.

1 Answer 1

2

You want -c instead of -e.

-e turns on "echo queries"

-c runs the command and exits

Have you considered putting just the create role command in a file called create_role.sql and copying that into /docker-entrypoint-initdb.d/?

Based on testing, it looks like an equivalent but simpler solution is to put the SQL command as one line in a file, 00_roles.sql, and copy that into the container instead of the init.sh script.

Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking that as an option based on what the entrypoint does, but i was trying to have 1 init script which processes it. I think i will give this an attempt as well. Do you have a sample file? I didnt think it would be as easy as just having that in an .sql file. I figured there might be other metadata? I was worried about the order of operations, since after it will do a pg_restore, BUT it seems that it will do SQL/DB files, then Executable SH files, then Source Non executable SH files.
@Fallenreaper I just tried it by putting create role yaml with login encrypted password 'password'; on a single line inside of a file called 00_roles.sql and copied that instead of the init.sh. I connected to shell, su postgres, and \du+ showed the yaml role. The "00" on the filename will help you deal with order of operations within the predefined ordering of types of files.
yeah, This works and I think is the best approach!! :) Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.