Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 postgresql

  2. Run the command: ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

  3. Create two new aliases to start and stop your postgres server. They could look something like this:

    alias pg_start="launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
    alias pg_stop="launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
    
  4. Run the alias you just created: pg_start. Use this comment to start your database service.

    • alternatively, pg_stop stops your database service.
  5. Run the command: createdb `whoami`

  6. Connect to your postgres with the command: psql

  7. brew reinstall readline - if needed

  8. createuser -s postgres - fixes role "postgres" does not exist

  9. Test with psql command

    $ psql
    psql (10.0)
    Type "help" for help.
    
    ibraheem=# 
    

Details

What is this ln command I ran in my Terminal?

from the man ln command

The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the copies''; instead, a link points'' to the original copy. There are two types of links; hard links and symbolic links. How a link ``points'' to a file is one of the differences between a hard and symbolic link.

What is launchctl?

from the man launchctl command

launchctl interfaces with launchd to manage and inspect daemons, angents and XPC services.

Commands

Create database

createdb <database_name>

createdb mydjangoproject_development

List databases

psql -U postgres -l

Show tables in database

psql -U postgres -d <database_name>

psql -U postgres -d mydjangoproject_development

Drop database

dropdb <database_name>

dropdb mydjangoproject_development

Restart database

dropdb <database_name> && createdb <database_name>

dropdb mydjangoproject_development && createdb mydjangoproject_development

@nydiarra

This comment has been minimized.

Copy link

commented Aug 20, 2018

I like your post. Very useful.

@Piglacquer

This comment has been minimized.

Copy link

commented Sep 10, 2018

Nice post, one little typo: Between steps 3 & 4 the aliases are different. Step 3 is pg_start // pg_stop, and step 4 is pg-start // pg-stop. Thanks!

@sandro-git

This comment has been minimized.

Copy link

commented Sep 22, 2018

thanks it help me a lot

@leftsider

This comment has been minimized.

Copy link

commented Oct 17, 2018

Simple and clear. Cheers!

@cyzanfar

This comment has been minimized.

Copy link

commented Oct 18, 2018

awesome stuffffff

@Darius-Ndubi

This comment has been minimized.

Copy link

commented Nov 6, 2018

Works like a charm.

@sergeev

This comment has been minimized.

Copy link

commented Nov 21, 2018

Thx! очень помогли!

@Physsix27

This comment has been minimized.

Copy link

commented Dec 11, 2018

Super helpful, thank you so much 👍 💯

@Sylvance

This comment has been minimized.

Copy link

commented Dec 13, 2018

Works like a charm.

True.

@balanka

This comment has been minimized.

Copy link

commented Dec 31, 2018

Login as default PostgreSQL user (postgres)

sudo -u postgres -i

@BesatZardosht

This comment has been minimized.

Copy link

commented Jan 9, 2019

I had to change step 3 to make it work:

alias pg_start="launchctl load ~/Library/LaunchAgents"
alias pg_stop="launchctl unload ~/Library/LaunchAgents"
@AyoAlfonso

This comment has been minimized.

Copy link

commented Jan 17, 2019

I think you meant to write 'pg_start' you wrote 'pg-start' instead.
Thanks for this guide Ibraheem.

@rajeshksv

This comment has been minimized.

Copy link

commented Jan 27, 2019

7th step - Read carefully. Don't reinstall readline until and unless needed.

I did it and it broke my whole system. Bash, Awk commands stopped working. Had to do "brew upgrade" to fix the issue.

@meniltiac

This comment has been minimized.

Copy link

commented Feb 5, 2019

Thanks so much for putting this together! It looks like the brew command has been updated, though. It's now brew install postgres and not brew install postgresql.

@ibraheem4

This comment has been minimized.

Copy link
Owner Author

commented Feb 16, 2019

Nice post, one little typo: Between steps 3 & 4 the aliases are different. Step 3 is pg_start // pg_stop, and step 4 is pg-start // pg-stop. Thanks!

Thanks for catching this, updated :)

@ibraheem4

This comment has been minimized.

Copy link
Owner Author

commented Feb 16, 2019

7th step - Read carefully. Don't reinstall readline until and unless needed.

I did it and it broke my whole system. Bash, Awk commands stopped working. Had to do "brew upgrade" to fix the issue.

Thanks for the comment, I've bolded if needed so hopefully others don't have this issue.

@ibraheem4

This comment has been minimized.

Copy link
Owner Author

commented Feb 16, 2019

Thanks so much for putting this together! It looks like the brew command has been updated, though. It's now brew install postgres and not brew install postgresql.

Thanks for the comment, I've updated to postgres instead of postgresql since it's the latest / greatest naming :)

@ganga549

This comment has been minimized.

Copy link

commented Feb 20, 2019

Hi,
I am getting error at Step 5 - '-bash: createdb: command not found'

@leodotng

This comment has been minimized.

Copy link

commented Feb 24, 2019

There's a gazillion ways to install this is the only guide that I don't run into errors first time around. Thank you for crystal clear steps!

@wardch

This comment has been minimized.

Copy link

commented Mar 7, 2019

Thanks a million for this gist, so straighforward. Got me rolling with a new db when I was having issues with set up

@arshpreetsingh

This comment has been minimized.

Copy link

commented Mar 14, 2019

Thanks! You rocks! saved my life ;)

@GUIEEN

This comment has been minimized.

Copy link

commented Mar 24, 2019

Thank you for your nice summary. But I'm curious that is there a reason you don't use brew services start/stop postgresql instead of making alias in shell ? I think controlling services installed by brew with brew services makes much more easier and simple.

@2lei

This comment has been minimized.

Copy link

commented Apr 19, 2019

truly useful,thanks

@sinanaltaii

This comment has been minimized.

Copy link

commented May 19, 2019

Thank you! Very useful and helped me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.