Setup Local PostgreSQL Server on Docker - gecko-8/devwiki GitHub Wiki

Up

Setup

  1. Download and install Docker for Mac
  2. Pull down the latest image for Postgres (optional, will be pulled by next command if you don’t have it)
    sudo docker pull postgres
    
  3. Create and run the container
    docker run --name postgres-server -v /Data/Databases/Postgres:/var/lib/postgresql/data -e POSTGRES_PASSWORD=<password> -p 5432:5432 -d postgres:latest
    
    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”

Connecting

  1. To connect an external program, just use localhost, port of 5432, user of postgres, and no password.

Deleting

  1. Stop the container
    docker stop postgres-server
    
  2. Delete the container
    docker rm postgres-server
    
  3. Delete the data folder if you want. E.g. /Data/Databases/Postgres
  4. Delete unused images (optional)
    docker image prune -a -f
    

Execute a SQL Script From a .sql File

  1. Copy the script into the local data location from above (e.g. /Data/Databases/Postgres)
  2. Connect to bash in your container:
    docker exec -it postgres-server bash
    
  3. Start psql and execute the script with the command
    psql -U postgres -f <filename>.sql
    
  4. Enter “exit” to leave the container.
⚠️ **GitHub.com Fallback** ⚠️