Running PostgreSQL in Docker on my Mac
Run PostgreSQL in Docker on macOS, with container setup notes and the original follow-up collection of PostgreSQL commands and examples.
It’s extremely convenient to run your local tools, like RDMS, development environments and so on on Docker containers on Mac. To be honest with you, I hate installing a bunch of software on my Mac, and that’s the main reason why do I use containers for my local development.
Before doing that manual, make sure your Docker is installed on Mac. To do so, you can go to https://www.docker.com/products/docker-desktop and download the latest version of Docker for your Mac.
So, how do I can use PostgreSQL on Mac in a Docker container? Let’s create a new Project folder, or you may use your own where docker-compose files.
mkdir ~/Projects
cd ~/Projects
And creating a docker-compose.yml file with the following code:
#File: docker-compose.yml
version: "3"
services:
db:
image: "postgres:17"
container_name: "local_postgres"
ports:
- "127.0.0.1:54320:5432"
volumes:
- dbdata:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=somesecretpassword
volumes:
dbdata:
So, a quick clarification on what we do here. We use postgres:17 image, naming containers as local_postgres, expose port 54320 on localhost only, store data on dbdata volume, and use somesecretpassword as password (change it to your own). Pretty straight forward, ha?
Update, November 30, 2026: the first version of this file had POSTGRES_HOST_AUTH_METHOD=trust and published the port on all interfaces. With trust Postgres doesn't ask for the password even if you set one, so other machines on the same network could get in as postgres. It also used postgres:11, which is end of life. If you used that version, changing the file is not enough: the image writes pg_hba.conf only when the data volume is empty, so trust stays there, and Postgres 17 won't start on a volume created by 11. Take a dump with pg_dumpall from the old container first, then recreate the volume and restore. The new file was not tested on a Mac with this update.
Ok, let’s build it and run it:
docker-compose up -d
You shouldn’t see any errors, and let’s see logs with the following command:
docker logs -f local_postgres
And if we can connect to database, like this:
docker exec -it local_postgres psql -U postgres
Hit CTRL+D to exit from the prompt.
Create a database
So, to create a database, you can run the following command, where new_database is database new you want to create.
docker exec -it local_postgres psql -U postgres -c "create database new_database"
You can use a database connection now with the following login details:
host='localhost',
port=54320,
dbname='new_database',
user='postgres',
password='somesecretpassword'
That’s pretty much it. Easy, right?
Please comment below if you have any issues or questions!
PostgreSQL Notes
Originally published April 20, 2020.
How to drop all tables sharing the same prefix in PostgreSQL?
DO
$do$
DECLARE
_tbl text;
BEGIN
FOR _tbl IN
SELECT quote_ident(table_schema) || '.'
|| quote_ident(table_name)
FROM information_schema.tables
WHERE table_name LIKE '[prefix]' || '%'
AND table_schema NOT LIKE 'pg\_%'
LOOP
-- RAISE NOTICE '%',
EXECUTE 'DROP TABLE ' || _tbl || ' CASCADE';
END LOOP;
END
$do$;
- Non-Cascade version:
EXECUTE 'DROP TABLE ' || _tbl;
How to drop all functions in PostgreSQL?
DO
$do$
DECLARE
_sql text;
BEGIN
SELECT INTO _sql
string_agg(format('DROP %s %s;'
, CASE prokind WHEN 'a' THEN 'AGGREGATE' WHEN 'p' THEN 'PROCEDURE' ELSE 'FUNCTION' END
, oid::regprocedure)
, E'\n')
FROM pg_proc
WHERE pronamespace = 'public'::regnamespace;
IF _sql IS NOT NULL THEN
RAISE NOTICE '%', _sql;
-- EXECUTE _sql;
ELSE
RAISE NOTICE 'No functions found in schema public';
END IF;
END
$do$ LANGUAGE plpgsql;