Skip to content

Instantly share code, notes, and snippets.

@JaveedIshaq
Created August 4, 2024 04:48
Show Gist options
  • Save JaveedIshaq/afdc8ee9b579f10711acfee685a6aff0 to your computer and use it in GitHub Desktop.
Save JaveedIshaq/afdc8ee9b579f10711acfee685a6aff0 to your computer and use it in GitHub Desktop.

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:

1. Create a Launch Daemon Plist File

You need to create a property list (plist) file that defines the behavior of the PostgreSQL service.

  1. Open Terminal.
  2. 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
  1. 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>
  1. Save the file and exit the editor (Ctrl + X, then Y, then Enter).

2. Load the Launch Daemon

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

3. Verify the Service

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.

4. Manually Start and Stop the Service (if needed)

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

5. Unload the Launch Daemon (if needed)

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:

Check PostgreSQL Status

Make sure PostgreSQL is running:

ps aux | grep postgres

You should see processes related to PostgreSQL if it's running.

Review Logs

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

Test Startup

Restart your Mac and verify that PostgreSQL starts automatically:

  1. Reboot your Mac.

  2. Check if PostgreSQL is running:

    ps aux | grep postgres

Additional Tips

  • Permissions: Ensure that the /Library/PostgreSQL/16/data directory and its contents are accessible by the postgres 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.

Next Steps

  1. 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
  2. Check Logs

    • Review the PostgreSQL log file to ensure there are no errors:

      cat /Library/PostgreSQL/16/data/server.log
  3. Test Database Connection

    • Test if you can connect to the PostgreSQL database:

      psql -U postgres
  4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment