Skip to content

Instantly share code, notes, and snippets.

@sgnl
Last active April 21, 2024 23:18
Show Gist options
  • Save sgnl/609557ebacd3378f3b72 to your computer and use it in GitHub Desktop.
Save sgnl/609557ebacd3378f3b72 to your computer and use it in GitHub Desktop.
Installing Postgres via Brew (OSX) (outdated see top most note)

Outdated note: the process is a lot easier now: after you brew install postgresql you can initialize or stop the daemon with these commands: brew services start postgresql or brew services stop postgresql.

new out put may look like

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

thank you commenters (sorry I didnt notice you all before but thankful for not getting notifications 🤭)

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. Read the Caveats section that is outputted to the Terminal. (out dated?)

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

  4. Open your zsh config file: subl ~/.zshrc

  5. At the bottom of the file, 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"
    
  6. Run the command: source ~/.zshrc to reload your configuration.

  7. Run the alias you just created: pg-start. Use this comment to start your database service.

    • alternatively, pg-stop stops your database service.
  8. Run the command: createdb `whoami`

  9. Connect to your postgres with the command: psql

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.

@sgnl
Copy link
Author

sgnl commented Apr 16, 2016

Ubuntu Installation Process:

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Once that is complete run the command: psql

You will see an error. Read the error, what is it saying?

It says that the "...role [yourusername] doesn't exist" so we will create one with the needed permissions.

enter the command: $ sudo -u postgres -c 'psql' this should log you into the postgres server.

To create a user, enter this command and replace yourusername with your username from the error message previous: CREATE ROLE yourusername WITH LOGIN CREATEDB CREATEDB

enter: \q to exit out

You should be able to run the command psql now and have permissions to create/modify/delete databases and roles.

@jaywon
Copy link

jaywon commented Jan 19, 2017

To summarize from the previous two comments this is all you need to do:
$ sudo su - postgresql
$ createuser --interactive
(enter your machine username at the first prompt and y at the second)
$ createdb yourusername
$ exit
$ psql //This will allow you to be able to run this psql command as your user and connect to Postgres

@vaibhav-plackal
Copy link

You say Connect with psql. What is the password for the admin account (postgres)?

@humoyun
Copy link

humoyun commented Aug 7, 2017

not working, bad solution

@Patticus3rd
Copy link

I cant find the zsh file

@ClarenceL
Copy link

ClarenceL commented Sep 5, 2017

If you're using bash, your equivalent of the zsh file would be ~/.bash_profile you can add aliases there and they will be there for you interactive shells in OSX terminal

After editing that file, type source ~/.bash_profile to init the aliases, but it'll be there for any new terminals you open.

@swaraj89
Copy link

swaraj89 commented Oct 10, 2017

I followed the exact steps. To my disappointment, am getting the following issues.

  1. -bash: pg-start: command not found
    added alias in .bashrc
  2.   dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
       Referenced from: /usr/local/lib/libpq.5.dylib
       Reason: image not found
       Abort trap: 6
    

@daaain
Copy link

daaain commented Dec 11, 2017

This seems to be outdated now, Brew output has:

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

@adiakritos
Copy link

What Caveats section?

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