Photo by Craig Stevenson on Unsplash
Mac Docker and Postgres - a Quick Start Guide
A quick and easy guide on getting up and running with Postgres on your local mac
Running Postgres on your mac doesn't have to be hard. Let's look at a super fast way to get up and running with Postgres.
1) Install Docker
Head over to the Docker Website, sign up, install docker, and login to the docker app. Now open a terminal and punch in these commands:
docker version
# You should see a version output. This may be large.
docker compose version
# Again, you should see a version. This will likely be a shorter output
2) Spin up Postgres in a Docker Container
Open a new folder in an IDE of your choice. Create a file in the root of that folder called docker-compose.yml
and paste the below yaml code:
version: "3.9"
services:
# Our Postgres database
db: # The service will be named db.
image: postgres # The postgres image will be used
restart: always # Always try to restart if this stops running
environment: # Provide environment variables
POSTGRES_USER: johndoe # POSTGRES_USER env var w/ value johndoe
POSTGRES_PASSWORD: astrongpassword
POSTGRES_DB: yournewdatabase
ports: # Expose ports so that apps not running via docker compose can connect to them.
- 5432:5432 # format here is "port on our machine":"port on container"
# Adminer provides a nice little web UI to connect to databases
adminer:
image: adminer
restart: always
environment:
ADMINER_DESIGN: dracula # Pick a theme
ports:
- 3333:8080
Now with that saved, open a terminal in the root of your folder and type docker compose up
. This will spin up a docker container and, if everything goes well, you should see a log stating database system is ready to accept connections
in the terminal.
3) Connect to Your New Docker Container
Open a new terminal tab and execute the following command:
docker compose exec -it db psql -U johndoe -d yournewdatabase
This will connect you to your database and now you'll be able to execute Postgres commands directly from the terminal.
Fin
That's the long short. Happy Postgresing!