Created
January 21, 2019 02:03
-
-
Save jefrydco/09476e0e1734635ceaa27d1eb1e3c385 to your computer and use it in GitHub Desktop.
Run PostgreSQL and pgAdmin4 on Docker with single command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create postgres directory with pgvolume and pga4volume as child directory. | |
mkdir -p postgres/{pgvolume,pga4volume} | |
# Navigate to postgres directory | |
cd postgres | |
# Create pg.env that holds environment variable for postgres docker container. | |
cat << EOF > pg.env | |
POSTGRES_USER=myusername | |
POSTGRES_PASSWORD=mypassword | |
EOF | |
# Create pga4.env that holds environment variable for pgadmin docker container. | |
cat << EOF > pga4.env | |
[email protected] | |
PGADMIN_DEFAULT_PASSWORD=mypassword | |
EOF | |
# Taken from: https://stackoverflow.com/questions/48643466/docker-create-network-should-ignore-existing-network#answer-53052379 | |
# Check if mypgnetwork exist or if itsn't create docker network named mypgnetwork | |
docker network inspect mypgnetwork &>/dev/null || \ | |
docker network create --driver bridge mypgnetwork | |
# Run postgres docker container | |
docker run --publish 5432:5432 \ | |
--volume $(pwd)/pgvolume:/var/lib/postgresql/data \ | |
--env-file ./pg.env \ | |
--name "mypostgres" \ | |
--hostname "mypostgres" \ | |
--network "mypgnetwork" \ | |
--detach \ | |
postgres:latest | |
# Run pgadmin docker container | |
docker run --publish 5050:80 \ | |
--volume $(pwd)/pga4volume:/var/lib/pgadmin \ | |
--env-file ./pga4.env \ | |
--name "mypgadmin4" \ | |
--hostname "mypgadmin4" \ | |
--network "mypgnetwork" \ | |
--detach \ | |
dpage/pgadmin4:latest | |
# Now, open up your browser on localhost:5050. Use [email protected] and mypassword to login. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script. For my environment I had to adjust permissions on the
pga4volume
directory. I probably didn't have to for thepgvolume
because I used my login user id forPOSTGRES_USER
.