Don’t install PostgreSQL

Especially don’t install PostgreSQL on your local workstation!

To do any meaningful coding, eventually you will have to move on from SQLite and work directly with a “Real Database”. I don’t have anything against, MySQL or MariaDB but I’ve always reached for PostgreSQL first. Unfortunately, many tutorials or videos will recommend you install PostgreSQL to learn how to use it. Don’t do that.

Run it in a Docker container. Sounds simple, but the devil is in the details. Well, I have created a docker-compose.yaml file which should have most of the details to give you a working and persistent database.

$ cat docker-compose.yaml
# Use postgres/example user/password credentials
version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: <change_me_to_something_secure>
    volumes:
      - /<path_on_local_filesystem>:/var/lib/postgresql/data
    ports:
      - 5432:5432

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

As of writing this, the most recent version of PostgreSQL is 15. If you want a specific version of PostgreSQL, check the full documentation for the image at: https://github.com/docker-library/docs/blob/master/postgres/README.md

You’ll notice that the most recent “Docker image” of PostgreSQL creates TWO running containers, the “adminer” container is a small web-gui running locally at http://127.0.0.1:8080. I have not investigated whether or not you can turn it off if desired…

At this point, you can use a local “psql” client to connect, but unfortunately the version provided by my OS is far behind.

$ psql -h localhost -U postgres
Password for user postgres:
psql (12.14 (Ubuntu 12.14-0ubuntu0.20.04.1), server 15.2 (Debian 15.2-1.pgdg110+1))
WARNING: psql major version 12, server major version 15.
         Some psql features might not work.

EDIT – You can install a PostgreSQL “psql” client at release 15 for Ubuntu by adding the PostgreSQL apt repository. Directions are at https://www.postgresql.org/download/linux/ubuntu/

To use “psql” inside the container.

# Just shell into the container
docker exec -it postgresql-docker-db-1 bash
# when inside
psql -U postgres

Finally, if you want an even easier way to run PostgreSQL and don’t mind it living in “the cloud” instead of your desktop, simply head to https://railway.app/new


Posted

in

by

Tags:

Comments

One response to “Don’t install PostgreSQL”

  1. […] an earlier blog post, I discussed running PostgreSQL in a container and noted the latest official image was bundled with […]

Leave a Reply

Your email address will not be published. Required fields are marked *