To configure PostgreSQL to start automatically on system boot on your Mac, you can use launchd
, which is the service management framework used by macOS. Here's how you can set it up:
You need to create a property list (plist) file that defines the behavior of the PostgreSQL service.
- Open Terminal.
- Create a new plist file in the
/Library/LaunchDaemons/
directory. You will need superuser privileges to create a file in this directory.
sudo nano /Library/LaunchDaemons/com.postgresql.postgres.plist
- Add the following content to the file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.postgresql.postgres</string>
<key>ProgramArguments</key>
<array>
<string>/Library/PostgreSQL/16/bin/pg_ctl</string>
<string>start</string>
<string>-D</string>
<string>/Library/PostgreSQL/16/data</string>
<string>-l</string>
<string>/Library/PostgreSQL/16/data/server.log</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>UserName</key>
<string>postgres</string>
</dict>
</plist>
- Save the file and exit the editor (Ctrl + X, then Y, then Enter).
To load the new Launch Daemon and start PostgreSQL automatically on boot, use the following command:
sudo launchctl load /Library/LaunchDaemons/com.postgresql.postgres.plist
You can verify that the service is loaded correctly and check its status:
sudo launchctl list | grep com.postgresql.postgres
You should see an entry for com.postgresql.postgres
if it was loaded successfully.
To manually start or stop the PostgreSQL service, you can use the following commands:
# To start the service
sudo launchctl start com.postgresql.postgres
# To stop the service
sudo launchctl stop com.postgresql.postgres
If you ever need to disable the auto-start, you can unload the plist:
sudo launchctl unload /Library/LaunchDaemons/com.postgresql.postgres.plist
With these steps, PostgreSQL should now be configured to start automatically on system boot on your Mac.
It looks like the Launch Daemon was successfully loaded, and the service is registered. However, to ensure everything is working correctly, you should verify a few things:
Make sure PostgreSQL is running:
ps aux | grep postgres
You should see processes related to PostgreSQL if it's running.
Check the PostgreSQL log file specified in the plist (/Library/PostgreSQL/16/data/server.log
) for any errors or messages:
cat /Library/PostgreSQL/16/data/server.log
Restart your Mac and verify that PostgreSQL starts automatically:
-
Reboot your Mac.
-
Check if PostgreSQL is running:
ps aux | grep postgres
- Permissions: Ensure that the
/Library/PostgreSQL/16/data
directory and its contents are accessible by thepostgres
user. Incorrect permissions might prevent PostgreSQL from starting. - Configuration: Double-check that the paths and configurations in the plist file are correct.
If PostgreSQL does not start automatically after a reboot, review the log files and plist configuration to diagnose any issues.
It looks like PostgreSQL is running as expected with the process /Library/PostgreSQL/16/bin/postgres -D /Library/PostgreSQL/16/data
. The process is listed in the output of ps aux
, indicating that it's active.
-
Verify Automatic Start on Boot
-
Restart your Mac to confirm that PostgreSQL starts automatically.
-
After the restart, check if PostgreSQL is running with:
ps aux | grep postgres
-
-
Check Logs
-
Review the PostgreSQL log file to ensure there are no errors:
cat /Library/PostgreSQL/16/data/server.log
-
-
Test Database Connection
-
Test if you can connect to the PostgreSQL database:
psql -U postgres
-
-
Troubleshooting
-
If PostgreSQL does not start automatically, ensure the plist file is correctly configured:
-
Confirm that the paths and permissions are correct.
-
Ensure that the plist file is correctly loaded with:
sudo launchctl list | grep com.postgresql.postgres
-
Check for any errors or warnings related to the plist configuration in the system logs:
sudo log show --predicate 'process == "launchd"' --info
-
-
Verify that the
postgres
user has the necessary permissions to access and start PostgreSQL.
-
These steps should help you confirm and ensure that PostgreSQL is set up to start automatically on your Mac.