Skip to content

Instantly share code, notes, and snippets.

@danny-andrews
Created March 3, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danny-andrews/59ba807f1074425f0a14345700a0745a to your computer and use it in GitHub Desktop.
Save danny-andrews/59ba807f1074425f0a14345700a0745a to your computer and use it in GitHub Desktop.
Installing Postgres on Mac

Install SQL using Homebrew

Begin by updating brew:

brew update

To install the latest version of PostgreSQL using Homebrew, run the following commands.

brew install postgresql

To install the latest version of MySQL using Homebrew, run the following commands.

brew install mysql

Exercise

Verify the latest version of PostgreSQL was installed correctly by running the following commands.

postgres --version
psql --version

Verify the latest version of MySQL was installed correctly by running the following commands.

mysql --version

How do you start a Server?

PostgreSQL using Homebrew Services

During installation, Homebrew will automatically initialize a PostgreSQL cluster on your machine. You can see how a PostgreSQL cluster is organized on the filesystem by running the following command.

ls -hal /usr/local/var/postgres/

And you should see something like this.

NOTE: When an error is generated by a PostgreSQL server managing this cluster, the error message is logged to a server.log file in this directory.

To manage the databases and tables inside a PostgreSQL cluster, you need a PostgreSQL server. There are a bunch of ways to start a server for this cluster, but one of the easiest ways is to launch it as a service.

A service is any server application that's launched as a background process when an operating system boots up. Once launched, the operating system will restart the service if it crashes. In other words, you start the service once and the operating system will keep it running indefinitely. The only way a service stops running is if you command the operating system to do so. As you can imagine, this is a very popular strategy for running a server in a production environment.

To see all the services running on your own machine, open the Activity Monitor application with Spotlight.

NOTE: A service can be launched by any user, including the root user account and your own user account.

The Homebrew Services plugin makes it a easy to manage services that are installed with Homebrew. To get started, install the Homebrew Services plugin with the following command.

brew tap homebrew/services

Then, use the plugin to list all the running services.

brew services list

And you should see something like this.

To start a PostgreSQL server as a service, run the following command.

brew services start postgresql

And you should see something like this.

To verify the server has started, check the list of running services.

brew services list

And you should see something like this.

To stop the service, run the following command.

brew services stop postgresql

And you should see something like this.

To verify the server has stopped, check the list of running services one more time.

brew services list

And you should see something like this.

MySQL

mysql.server start

to stop the MySQL server:

mysql.server stop

If you attempt to use the MySQL client but have not run mysql.server start you will see an error message like:

$: > mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Exercise - PostgreSQL

Using the Homebrew Service plugin, start a PostgreSQL server for the default PostgreSQL cluster.

When you're done, check out the plugin's usage message with the following command.

brew services --help

Resources

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