The contrib/start-scripts/macos directory of the PostgreSQL source code contains files and instructions to automatically launch the postgres server at system start on macOS.

The supplied shell script and plist already match the default settings for a standard installation.

If you’ve installed postgres following the directions on this site, just copy the script and .plist file in contrib/start-scripts/macos to the appropriate system locations:

% cd ~/sandbox/postgresql-14.1/contrib/start-scripts/macos
% sudo cp postgres-wrapper.sh /usr/local/pgsql/bin
% sudo cp org.postgresql.postgres.plist /Library/LaunchDaemons/

To test without restarting your computer, load the plist with launchctl:

% sudo launchctl load /Library/LaunchDaemons/org.postgresql.postgres.plist

Use the unload option to stop the server:

% sudo launchctl unload /Library/LaunchDaemons/org.postgresql.postgres.plist

Due to the KeepAlive setting in the plist file, the PostgreSQL server will restart automatically if you stop it using normal methods. This was a very funny thing to learn the hard way!