Skip to content

Instantly share code, notes, and snippets.

@tristanlins
Last active February 18, 2021 01:40
Show Gist options
  • Save tristanlins/4491f54ba06d18661fe2204592cdf54e to your computer and use it in GitHub Desktop.
Save tristanlins/4491f54ba06d18661fe2204592cdf54e to your computer and use it in GitHub Desktop.
# @file /etc/systemd/system/docker-postgres.service
# @see https://docs.docker.com/articles/host_integration/
[Unit]
Description=postgres container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a postgres
ExecStop=/usr/bin/docker stop -t 60 postgres
[Install]
WantedBy=multi-user.target
# start container
sudo systemctl start docker-postgres
# stop container
sudo systemctl stop docker-postgres
# attach shell to container
docker exec -it postgres bash
# start psql connection
docker exec -it postgres psql -h postgres -U postgres
# dump a database
docker exec postgres pg_dump -h postgres -U postgres ${table}
# see the logs
docker logs postgres
# for more see
# https://hub.docker.com/_/postgres/
# https://docs.docker.com/articles/host_integration/
# create config and data directory
mkdir -p $HOME/docker/postgres/data
# initially create the docker container
docker run --restart=on-failure:5 \
--volume=$HOME/docker/postgres/data:/var/lib/postgresql/data \
--env=POSTGRES_PASSWORD=secret \
--publish=5432:5432 \
--hostname=postgres \
--name=postgres \
--detach=true \
postgres:latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment