There are many guides around for installing and running postgres on macOS/OSX, a lot of them are old and almost all of them do something drastically differently. This is my process for installing and managing a postgres install which follows as closely to the official postgres docs as I can. My hopes is that this provides a clean and simple installation.
I have one requirement aside from the ones mentioned above, and that is the use of homebrew. If you've not used homebrew before, it's a very simple command line package manager for macOS/OSX. Homebrew installations are provided by the official Postgres project so I feel this fits nicely.
- Ensure homebrew is installed and up-to-date:
$ brew doctor $ brew update
- Install the
$ brew install postgresql
Again, for this I'll be using homebrew because it simplifies things for us rather than managing systemctl or something similar. Daemon managers are all great tools, but I'm aiming for simplicity here.
- Startup the postgres service with homebrew
$ brew services start postgresql
Setting up Postgres
Some people will tell you to use
createdb, this is generally a good way to create a new database but the postgres docs state
The first database is always created by the initdb command when the data storage area is initialized.
So we'll go ahead and use the
- Run initdb - I'm going to point the data directory (where postgres stores all our data) to
/usr/local/var/postgres, I'm also going to set the encoding to
utf8. These are sane settings for my use (local web app development) but your usage may require some different settings. Use
initdb --helpto see the options.
$ initdb /usr/local/var/postgres -E utf8
initdb creates a default database called
postgres. It actually also creates another called
template1 but that's a system database you shouldn't touch (unless you know what you're doing).
Connecting to our new database
We now have postgres installed and running with a default database initialised. We can connect to our database using the
psql client, this is just a simple terminal postgres terminal client. We can connect to the our default database with:
$ psql -h localhost -d postgres
This will open a connection to our default database where we can start cracking out some SQL to our hearts content.
If you'd rather use another client, I'm a fan of DataGrip, your credentials will be as follows.
host: localhost port: 5432 database: postgres
The username and password will be empty.