0

Though default Postgres Image satisfies my needs I'd like to personalise the database and users inside it.

After pulling the image database has one user postgres without any password.

Assuming that this code runs the image:

docker run -p 5432:5432 postgres

How do I run psql command inside it? (to delete 'postgres' user, create new database, more users etc.)

2 Answers 2

1

Run psql on another container and link it:

$ docker run -it --rm --link <running-postgres-container-id> postgres psql -h postgres -U postgres

Or, run psql on the same container:

$ docker exec -it <running-postgres-container-id> psql -U postgres
Sign up to request clarification or add additional context in comments.

Comments

0

The official postgres image accepts environment variables to create a custom super user with a password and a custom db name.

If you do not wish to provide these variables when running the container what you can do is create an image using the official postgres image as the base, add a bash script which sets the appropriate environment variables and calls entrypoint script docker-entrypoint.sh.

If you wish to add more databases or users, you may want to replace the script entirely with your own (the file is located under /usr/local/bin/ within the container). The following lines would be of interest to you:

"${psql[@]}" --username postgres <<-EOSQL
            CREATE DATABASE "$POSTGRES_DB" ;
        EOSQL

"${psql[@]}" --username postgres <<-EOSQL
        $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;

Comments

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.