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 postgres

  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"
         alias pg_stop="launchctl unload ~/Library/LaunchAgents"
    
  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 - ONLY 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

@NguyenHiep
Copy link

NguyenHiep commented Oct 3, 2020

Thanks.

@dagrv
Copy link

dagrv commented Oct 11, 2020

Thanks;

@maherelgamil
Copy link

maherelgamil commented Oct 13, 2020

Thanks πŸ™ˆ

@gpalazzo
Copy link

gpalazzo commented Oct 15, 2020

amazing tutorial, worked perfectly :)

@sreekaanth
Copy link

sreekaanth commented Nov 17, 2020

This was very useful thanks a lot!

@ahwinemman
Copy link

ahwinemman commented Dec 15, 2020

Thanks

@georgegrigorita
Copy link

georgegrigorita commented Dec 19, 2020

Thanks @ibraheem4 and @rclik!

@bkwaku
Copy link

bkwaku commented Feb 4, 2021

Great post.

Copy link

ghost commented Feb 6, 2021

Thanks! Great post!

@HubM
Copy link

HubM commented Feb 25, 2021

Amazing, thanks !

@zrtsky
Copy link

zrtsky commented Mar 1, 2021

I had to change step 3 to make it work:

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

πŸ”₯ πŸ™ 😌

@ibraheem4
Copy link
Author

ibraheem4 commented Mar 1, 2021

I had to change step 3 to make it work:

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

πŸ”₯ πŸ™ 😌

Updated πŸš€

@zrtsky
Copy link

zrtsky commented Mar 1, 2021

I had to change step 3 to make it work:

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

πŸ”₯ πŸ™ 😌

Updated πŸš€

Thanks πŸ™‚

@remilia-grimm
Copy link

remilia-grimm commented Jul 15, 2021

Oh!

For anyone getting launchctl load i/o errors

do:

launchctl unload ~/path
launchctl load ~/path

That should clear the error for BigSur+

And isn't "load" a legacy command now?

@C-Compton
Copy link

C-Compton commented Aug 13, 2021

Oh!

For anyone getting launchctl load i/o errors

do:

launchctl unload ~/path
launchctl load ~/path

That should clear the error for BigSur+

And isn't "load" a legacy command now?

I'm on Big Sur. I created the aliases as described above and I'm receiving
Load failed: 5: Input/output error

Your suggestion here didn't work either.

And to answer your question about the load option for launchctl, it is legacy per the man page.

LEGACY SUBCOMMANDS
     Subcommands from the previous implementation of launchd are generally available, though some may be unimplemented.
     Unimplemented subcommands are documented as such.

     load | unload [-wF] [-S sessiontype] [-D searchpath] paths ...
              Load the specified configuration files or directories of configuration files.  Jobs that are not on-demand...

@remilia-grimm
Copy link

remilia-grimm commented Aug 13, 2021

@C-Compton
Copy link

C-Compton commented Aug 18, 2021

Interesting! I wonder what is occurring, are you on an M1 or a Hackintosh? And I appreciate the man page clarification!
…

Native MacOS on Intel.

I just went with the brew services ... solution posted higher in this thread.

@Mahfuz2811
Copy link

Mahfuz2811 commented Aug 25, 2021

I am facing the same issue in MacOS BigSur

Load failed: 5: Input/output error

Does anyone find the solution?

@C-Compton
Copy link

C-Compton commented Aug 28, 2021

I am facing the same issue in MacOS BigSur

Load failed: 5: Input/output error

Does anyone find the solution?

@Mahfuz2811 Check out this response here: https://gist.github.com/ibraheem4/ce5ccd3e4d7a65589ce84f2a3b7c23a3#gistcomment-3443897

@ai4jenenerey
Copy link

ai4jenenerey commented Aug 28, 2021

I am facing the same issue in MacOS BigSur

Load failed: 5: Input/output error

Does anyone find the solution?

Stucked with this issue too.

@williammmussa
Copy link

williammmussa commented Aug 30, 2021

Hello everyone i try to create a user it returns no response but the is not created....what am i missing here

@dknopf
Copy link

dknopf commented Aug 30, 2021

I am facing the same issue in MacOS BigSur

Load failed: 5: Input/output error

Does anyone find the solution?

I'm also stuck here when running either the alias OR brew services start postgresql, and running brew services cleanup in the response by @C-Compton didn't seem to work either. Is there a way to revert the launchutl/ln commands that I ran because of this gist?

When trying to run the unload commands from @remilia-grimm I get Unload failed: 5: Input/output error

@jkapeniak
Copy link

jkapeniak commented Oct 3, 2021

I am facing the same issue in MacOS BigSur
Load failed: 5: Input/output error
Does anyone find the solution?

I'm also stuck here when running either the alias OR brew services start postgresql, and running brew services cleanup in the response by @C-Compton didn't seem to work either. Is there a way to revert the launchutl/ln commands that I ran because of this gist?

When trying to run the unload commands from @remilia-grimm I get Unload failed: 5: Input/output error

I also had the Unload failed: 5: Input/output error I was able to solve it and run it via brew services start from the following steps:

This was my link command:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

Which produced the following output:
/Users/yourusername/Library/LaunchAgents -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

As per this stack post you can remove by removing the thing to the left of the arrow:

sudo rm /Users/yourusername/Library/LaunchAgents

I was then able to run
brew services start postgresql

@Thihasoehlaing
Copy link

Thihasoehlaing commented Oct 8, 2021

That help me alot. Thanks

@joshades
Copy link

joshades commented Dec 6, 2021

This helps a lot. Thank you. :-)

@nileshpandey3
Copy link

nileshpandey3 commented Feb 7, 2022

  • This command brew services stop postgresql helped me resolve Input/output error
  • Then I could successfully start using pg_ctl -D /usr/local/var/postgres start

@developerbello
Copy link

developerbello commented Feb 10, 2022

Thank Man

@brianlens
Copy link

brianlens commented Apr 13, 2022

For M1 Macs, the directory changed a bit I see. usr/local/ should be replaced with /opt/homebrew/ when running step 2

@joy2323
Copy link

joy2323 commented May 6, 2022

As it is said here, it is not necessary to manage services manually that are explained on 2, 3 and 4 steps. Instead of these steps you can use these ones, I think, it is simpler and more manageable;

  • To start PostgreSQL as service use;
    brew services start postgresql
  • To stop PostgreSQL
    brew services stop postgresql
  • And if you un-install the postgresql then you can remove its links with that command;
    brew services cleanup

And also by doing this way, brew services maintains the status of the service. I mean; when you stop the service via brew services, then it is not automatically started on next system start up. But, on the other hand, if you do not use brew services, then the service (postgresql) always started up on the system start up.

Thank you @ibraheem4 and @rclik

@marciprescott
Copy link

marciprescott commented May 30, 2022

Thanks @joy2323

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