Skip to content

Instantly share code, notes, and snippets.

@DrTom
Last active July 1, 2025 02:22
Show Gist options
  • Save DrTom/4f2edcac26a0eae82360dbc9b18dd82c to your computer and use it in GitHub Desktop.
Save DrTom/4f2edcac26a0eae82360dbc9b18dd82c to your computer and use it in GitHub Desktop.

Revisions

  1. DrTom revised this gist Jul 26, 2024. 1 changed file with 54 additions and 0 deletions.
    54 changes: 54 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # PostgreSQL Server Setup on Mac OS with MacPorts

    This is in particular aimed at using multiple versions of PostgreSQL at different ports.

    export VERSION=15

    ## Install
    @@ -40,4 +42,56 @@ initially also as user `postgres`
    ```
    CREATE USER tom WITH SUPERUSER LOGIN PASSWORD 'tom';
    CREATE DATABASE tom owner tom
    ```


    ## My `bash` Config

    ```
    # POSTGRESQL ##################################################################
    function pg_env_set(){
    if [ -z $1 ]; then
    export PGVERSION=$1
    else
    export PGVERSION=${1:-"15"}
    fi
    if shopt -q login_shell; then
    save_echo "SETTING UP POSTGRES ENVIRONMENT FOR VERSION ${PGVERSION}"
    fi
    export POSTGRESQL="/opt/local/lib/postgresql${PGVERSION}"
    case $PGVERSION in
    *)
    export PGPORT="54${PGVERSION}"
    ;;
    esac
    export PATH="$POSTGRESQL/bin:$PATH"
    export PGUSER="tom"
    export PGPASSWORD="tom"
    export PGPIDNAME="pid"
    }
    function pg_start(){
    sudo -i -u postgres /opt/local/lib/postgresql${PGVERSION}/bin/pg_ctl \
    -D /opt/local/var/db/postgresql${PGVERSION}/defaultdb \
    -l /opt/local/var/log/postgresql${PGVERSION}/postgres.log \
    start
    }
    function pg_stop(){
    set -euo pipefail
    sudo -u postgres /opt/local/lib/postgresql${PGVERSION}/bin/pg_ctl \
    -D /opt/local/var/db/postgresql${PGVERSION}/defaultdb \
    stop
    }
    pg_env_set 15
    # END POSTGRESQL ##############################################################
    ```
  2. DrTom revised this gist Jul 26, 2024. 1 changed file with 1 addition and 31 deletions.
    32 changes: 1 addition & 31 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -21,22 +21,8 @@ sudo su postgres -c 'cd /opt/local/var/db/postgresql${VERSION} && /opt/local/lib

    change the `port` in `/opt/local/var/db/postgresql${VERSION}/defaultdb/postgresql.conf`

    ###
    our|my convention is to use "54${VERSION}", currently 5415

    ## Load and Unload

    **This seems to be broken** => manual start, see below

    ```
    port unload postgresql12-server
    port load postgresql12-server
    ```

    see also https://guide.macports.org/chunked/reference.startupitems.html#reference.startupitems.

    `/Library/LaunchDaemons/org.macports.postgresql12-server.plist`

    `/opt/local/etc/LaunchDaemons/org.macports.postgresql12-server/postgresql12-server.wrapper`

    ## Starting manually / Troubleshooting

    @@ -46,22 +32,6 @@ as user `postgres`, e.g. `sudo -i -u postgres`
    /opt/local/lib/postgresql${VERSION}/bin/pg_ctl -D /opt/local/var/db/postgresql${VERSION}/defaultdb start -l /opt/local/var/log/postgresql${VERSION}/postgres.log
    ```

    for version 10 pg server:

    oneliner:
    ```
    sudo -u postgres /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/postgresql10/defaultdb start -l /opt/local/var/log/postgresql10/postgres.log
    ```

    ```
    /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/postgresql10/defaultdb start -l /opt/local/var/log/postgresql10/postgres.log
    ```

    ### pg 13

    ```
    /opt/local/lib/postgresql13/bin/pg_ctl -D /opt/local/var/db/postgresql13/defaultdb -l /opt/local/var/log/postgresql13/postgres.log start
    ```

    ## Add User

  3. DrTom revised this gist Oct 21, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    ```
    port info postgresql${VERSION}-server
    port install postgresq${VERSION}-server
    port install postgresql${VERSION}-server
    ```

    ## Initial Setup
  4. DrTom revised this gist Mar 8, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,6 @@ sudo -u postgres /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/pos
    initially also as user `postgres`

    ```
    CREATE USER thomas WITH SUPERUSER LOGIN PASSWORD 'thomas';
    CREATE DATABASE thomas owner thomas
    CREATE USER tom WITH SUPERUSER LOGIN PASSWORD 'tom';
    CREATE DATABASE tom owner tom
    ```
  5. DrTom revised this gist Mar 8, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,8 @@ sudo su postgres -c 'cd /opt/local/var/db/postgresql${VERSION} && /opt/local/lib

    ```

    change the `port` in `/opt/local/var/db/postgresql${VERSION}/defaultdb/postgresql.conf`

    ###

    ## Load and Unload
  6. DrTom revised this gist Mar 8, 2023. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,26 @@
    # PostgreSQL Server Setup on Mac OS with MacPorts

    export VERSION=15

    ## Install

    ```
    port info postgresql12-server
    port install postgresql12-server
    port info postgresql${VERSION}-server
    port install postgresq${VERSION}-server
    ```

    ## Initial Setup

    ```sh

    sudo mkdir -p /opt/local/var/db/postgresql12/defaultdb
    sudo chown postgres:postgres /opt/local/var/db/postgresql12/defaultdb
    sudo su postgres -c 'cd /opt/local/var/db/postgresql12 && /opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db/postgresql12/defaultdb'
    sudo mkdir -p /opt/local/var/db/postgresql${VERSION}/defaultdb
    sudo chown postgres:postgres /opt/local/var/db/postgresql${VERSION}/defaultdb
    sudo su postgres -c 'cd /opt/local/var/db/postgresql${VERSION} && /opt/local/lib/postgresql15/bin/initdb -D /opt/local/var/db/postgresql15/defaultdb'

    ```

    ###

    ## Load and Unload

    **This seems to be broken** => manual start, see below
    @@ -37,7 +41,7 @@ see also https://guide.macports.org/chunked/reference.startupitems.html#referenc
    as user `postgres`, e.g. `sudo -i -u postgres`

    ```
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb start -l /opt/local/var/log/postgresql12/postgres.log
    /opt/local/lib/postgresql${VERSION}/bin/pg_ctl -D /opt/local/var/db/postgresql${VERSION}/defaultdb start -l /opt/local/var/log/postgresql${VERSION}/postgres.log
    ```

    for version 10 pg server:
  7. DrTom revised this gist Oct 23, 2021. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # PostgreSQL 12 Server Setup on Mac OS with MacPorts
    # PostgreSQL Server Setup on Mac OS with MacPorts

    ## Install

    @@ -51,6 +51,12 @@ sudo -u postgres /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/pos
    /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/postgresql10/defaultdb start -l /opt/local/var/log/postgresql10/postgres.log
    ```

    ### pg 13

    ```
    /opt/local/lib/postgresql13/bin/pg_ctl -D /opt/local/var/db/postgresql13/defaultdb -l /opt/local/var/log/postgresql13/postgres.log start
    ```

    ## Add User

    initially also as user `postgres`
  8. DrTom revised this gist Aug 26, 2021. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,11 @@ as user `postgres`, e.g. `sudo -i -u postgres`

    for version 10 pg server:

    oneliner:
    ```
    sudo -u postgres /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/postgresql10/defaultdb start -l /opt/local/var/log/postgresql10/postgres.log
    ```

    ```
    /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/postgresql10/defaultdb start -l /opt/local/var/log/postgresql10/postgres.log
    ```
  9. DrTom revised this gist Aug 24, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,12 @@ as user `postgres`, e.g. `sudo -i -u postgres`
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb start -l /opt/local/var/log/postgresql12/postgres.log
    ```

    for version 10 pg server:

    ```
    /opt/local/lib/postgresql10/bin/pg_ctl -D /opt/local/var/db/postgresql10/defaultdb start -l /opt/local/var/log/postgresql10/postgres.log
    ```

    ## Add User

    initially also as user `postgres`
  10. DrTom revised this gist Jun 24, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # PostgreSQL 12 Server Setup on Mac OS with MacPorts

    ## Install

    ```
  11. DrTom revised this gist Jun 24, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,8 @@ as user `postgres`, e.g. `sudo -i -u postgres`

    ## Add User

    initially also as user `postgres`

    ```
    CREATE USER thomas WITH SUPERUSER LOGIN PASSWORD 'thomas';
    CREATE DATABASE thomas owner thomas
  12. DrTom revised this gist Jun 24, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ sudo su postgres -c 'cd /opt/local/var/db/postgresql12 && /opt/local/lib/postgre

    ## Load and Unload

    ** This seems to be broken** => manual start, see below
    **This seems to be broken** => manual start, see below

    ```
    port unload postgresql12-server
  13. DrTom revised this gist Jun 24, 2020. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@ sudo su postgres -c 'cd /opt/local/var/db/postgresql12 && /opt/local/lib/postgre

    ## Load and Unload

    ** This seems to be broken** => manual start, see below

    ```
    port unload postgresql12-server
    port load postgresql12-server
    @@ -34,4 +36,11 @@ as user `postgres`, e.g. `sudo -i -u postgres`

    ```
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb start -l /opt/local/var/log/postgresql12/postgres.log
    ```

    ## Add User

    ```
    CREATE USER thomas WITH SUPERUSER LOGIN PASSWORD 'thomas';
    CREATE DATABASE thomas owner thomas
    ```
  14. DrTom revised this gist Jun 24, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,8 @@ see also https://guide.macports.org/chunked/reference.startupitems.html#referenc

    ## Starting manually / Troubleshooting

    as user `postgres`, e.g. `sudo -i -u postgres`

    ```
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb start -l /opt/local/var/log/postgresql12/postgres.log
    ```
  15. DrTom revised this gist Jun 22, 2020. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,8 @@ see also https://guide.macports.org/chunked/reference.startupitems.html#referenc

    `/opt/local/etc/LaunchDaemons/org.macports.postgresql12-server/postgresql12-server.wrapper`

    ## Troubleshooting
    ## Starting manually / Troubleshooting

    ```
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb -l logfile start
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb start -l /opt/local/var/log/postgresql12/postgres.log
    ```
  16. DrTom revised this gist Jun 22, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,11 @@ port install postgresql12-server
    ## Initial Setup

    ```sh

    sudo mkdir -p /opt/local/var/db/postgresql12/defaultdb
    sudo chown postgres:postgres /opt/local/var/db/postgresql12/defaultdb
    sudo su postgres -c '/opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db/postgresql12/defaultdb'
    sudo su postgres -c 'cd /opt/local/var/db/postgresql12 && /opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db/postgresql12/defaultdb'

    ```

    ## Load and Unload
  17. DrTom revised this gist Jun 22, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,8 @@ see also https://guide.macports.org/chunked/reference.startupitems.html#referenc

    `/Library/LaunchDaemons/org.macports.postgresql12-server.plist`

    `/opt/local/etc/LaunchDaemons/org.macports.postgresql12-server/postgresql12-server.wrapper`

    ## Troubleshooting

    ```
  18. DrTom revised this gist Jun 22, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,9 @@ port unload postgresql12-server
    port load postgresql12-server
    ```

    see also https://guide.macports.org/chunked/reference.startupitems.html#reference.startupitems.internals
    see also https://guide.macports.org/chunked/reference.startupitems.html#reference.startupitems.

    `/Library/LaunchDaemons/org.macports.postgresql12-server.plist`

    ## Troubleshooting

  19. DrTom revised this gist Jun 22, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,8 @@ port unload postgresql12-server
    port load postgresql12-server
    ```

    see also https://guide.macports.org/chunked/reference.startupitems.html#reference.startupitems.internals

    ## Troubleshooting

    ```
  20. DrTom revised this gist Jun 22, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -23,5 +23,6 @@ port load postgresql12-server
    ## Troubleshooting

    ```
    pg_ctl -D /opt/local/var/db/postgresql12/defaultdb/ start
    /opt/local/lib/postgresql12/bin/pg_ctl -D /opt/local/var/db/postgresql12/defaultdb -l logfile start
    ```
  21. DrTom revised this gist Jun 22, 2020. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@ port info postgresql12-server
    port install postgresql12-server
    ```


    ## Initial Setup

    ```sh
    @@ -19,4 +18,10 @@ sudo su postgres -c '/opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db
    ```
    port unload postgresql12-server
    port load postgresql12-server
    ```

    ## Troubleshooting

    ```
    pg_ctl -D /opt/local/var/db/postgresql12/defaultdb/ start
    ```
  22. DrTom revised this gist Jun 22, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,11 @@
    ## Install

    ```
    port info postgresql12-server
    port install postgresql12-server
    ```


    ## Initial Setup

    ```sh
  23. DrTom revised this gist Jun 22, 2020. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,14 @@
    ## Initial Setup

    ```sh
    sudo mkdir -p /opt/local/var/db/postgresql12/defaultdb
    sudo chown postgres:postgres /opt/local/var/db/postgresql12/defaultdb
    sudo su postgres -c '/opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db/postgresql12/defaultdb'
    ```

    ## Load and Unload

    ```
    port unload postgresql12-server
    port load postgresql12-server
    ```
  24. DrTom created this gist Jun 22, 2020.
    5 changes: 5 additions & 0 deletions MacPort PostgreSQL setup.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ```sh
    sudo mkdir -p /opt/local/var/db/postgresql12/defaultdb
    sudo chown postgres:postgres /opt/local/var/db/postgresql12/defaultdb
    sudo su postgres -c '/opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db/postgresql12/defaultdb'
    ```