Skip to content

Instantly share code, notes, and snippets.

@trangtungn
Forked from ibraheem4/postgres-brew.md
Last active September 28, 2022 19:10
Show Gist options
  • Save trangtungn/2111c861f28a45b88bb2c53774665546 to your computer and use it in GitHub Desktop.
Save trangtungn/2111c861f28a45b88bb2c53774665546 to your computer and use it in GitHub Desktop.
Installing Postgres via Brew (OSX)

Installing Postgres via Brew

Pre-Reqs

Brew Package Manager

In your command-line run the following commands:

  1. brew doctor
  2. brew update

Installing

  1. In your command-line run the command: brew install postgres

  2. Configuration can be found in this file: /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

  3. Create new aliases to start and stop postgres server (in .bachrc/.zshrc/etc.):

    alias pg_start="brew services start postgresql"
    alias pg_stop="brew services stop postgresql"
    
  4. Run the aliases:

  • to start database service

    $ pg_start
    
  • to check if database service running or not:

    $ brew services list
    

    OR

    $ ps aux | grep postgres
    
  • to stop database service.

    $ pg_stop
    
  1. Connect to database postgres with psql

    $ psql postgres
    psql (14.1)
    Type "help" for help.
    
    postgres=# 
    
  2. createuser -s postgres - fixes role "postgres" does not exist

  3. Create database: createdb `whoami`

Optional Configurations:

  1. Change data directory (DATADIR):
  • In the file "/usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist"

  • Change the default location, value after option -D, to a custom directory, e.g. "/Users/abc/postgres":

    <key>ProgramArguments</key>
    <array>
       <string>/usr/local/opt/postgresql/bin/postgres</string>
       <string>-D</string>
       <string>/Users/abc/postgres</string>
    </array>
    
  • Directoy "/Users/abc/postgres" must have either permission u=rwx (0700) or u=rwx,g=rx (0750):

    $ chmod 0750 /Users/abc/postgres
    
  1. Change log file directory:

    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/postgres.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/var/log/postgres.log</string>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment