Setup Local PostgreSQL Server on Docker - gecko-8/devwiki GitHub Wiki
- Download and install Docker for Mac
- Pull down the latest image for Postgres (optional, will be pulled by next command if you don’t have it)
sudo docker pull postgres
- Create and run the container
Note: This stores your data files in the folder /Data/Databases/Postgres. This can be changed to any location you want. Note: Default username will be “postgres”
docker run --name postgres-server -v /Data/Databases/Postgres:/var/lib/postgresql/data -e POSTGRES_PASSWORD=<password> -p 5432:5432 -d postgres:latest
- To connect an external program, just use localhost, port of 5432, user of postgres, and no password.
- Stop the container
docker stop postgres-server
- Delete the container
docker rm postgres-server
- Delete the data folder if you want. E.g. /Data/Databases/Postgres
- Delete unused images (optional)
docker image prune -a -f
- Copy the script into the local data location from above (e.g. /Data/Databases/Postgres)
- Connect to bash in your container:
docker exec -it postgres-server bash
- Start psql and execute the script with the command
psql -U postgres -f <filename>.sql
- Enter “exit” to leave the container.