Fallenreaper
Fallenreaper

Reputation: 10704

docker postgres init script seems will not create db user, command is ignored

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

Im not sure why this is happening, and when doing an interactive terminal, this command seemingly worked. I dont see any additional information and wasnt 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 '.

Upvotes: 1

Views: 757

Answers (1)

Mike Organek
Mike Organek

Reputation: 12494

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.

Upvotes: 2

Related Questions