Skip to content

Instantly share code, notes, and snippets.

@JaveedIshaq
Created August 4, 2024 04:48
Show Gist options
  • Select an option

  • Save JaveedIshaq/afdc8ee9b579f10711acfee685a6aff0 to your computer and use it in GitHub Desktop.

Select an option

Save JaveedIshaq/afdc8ee9b579f10711acfee685a6aff0 to your computer and use it in GitHub Desktop.

Revisions

  1. JaveedIshaq created this gist Aug 4, 2024.
    159 changes: 159 additions & 0 deletions configure-postgresql-auto-start.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,159 @@
    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.

    ```sh
    sudo nano /Library/LaunchDaemons/com.postgresql.postgres.plist
    ```

    3. Add the following content to the file:

    ```xml
    <?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>
    ```

    4. 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:

    ```sh
    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:

    ```sh
    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:

    ```sh
    # 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:

    ```sh
    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:

    ```sh
    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:

    ```sh
    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:

    ```sh
    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:

    ```sh
    ps aux | grep postgres
    ```

    2. **Check Logs**
    - Review the PostgreSQL log file to ensure there are no errors:

    ```sh
    cat /Library/PostgreSQL/16/data/server.log
    ```

    3. **Test Database Connection**
    - Test if you can connect to the PostgreSQL database:

    ```sh
    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:

    ```sh
    sudo launchctl list | grep com.postgresql.postgres
    ```

    - Check for any errors or warnings related to the plist configuration in the system logs:

    ```sh
    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.