Install PostgreSQL on Mac

There are multiple way we can install PostgreSQL on Mac machines

Pre-requisites

  1. Software
    1. On a Mac system, you must have superuser or sudo privileges
  2. Hardware – The minimum hardware required to install and run PostgreSQL are:
    1. 1 GHz Processor
    2. 2 GB RAM
    3. Minimum 512 MB Hard-disk Space

Note:- 512 MB hard-disk space only for PostgreSQL, if you need PgAdmin 4 you need additional space

Method 1 : Using Postres App (https://postgresapp.com/)

Postgres.app is a full-featured PostgreSQL installation packaged as a standard Mac app. It includes everything you need to get started.

This is the most easiest method to install PostgreSQL as this comes as a bundle which consists of PostgreSQL and PostGIS (Spatial Database of Postgres)

Step 1 : Download the postgres.app from https://postgresapp.com/downloads.html

I have downloaded PostgreSQL 14, see below, click on download

Move to Application folder and double click and install

Go to LaunchPad, find postgres and click on it

Click on “initialize”

Configure your $PATH to use the included command line tools (optional):

sudo mkdir -p /etc/paths.d &&echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

By deafult Postgres.app on Mac comes with 3 different databases, macbook, postgres and template

Double click on any of the database and it will open in command prompt, see below screenshot

Hostlocalhost
Port5432
Usermacbook
Databasemacbook or postgres or template
Passwordnone
Connection URLpostgresql://localhost

Congrajulations, we have successfully installed PostgreSQL on Mac Machine

Next step is to install PgAdmin which is a IDE for PostgreSQL

Download PgAdmin 4 from https://www.pgadmin.org/download/pgadmin-4-macos/

Download and install

Go to LaunchPad and open “PgAdmin”

Click on “Add New Server” and enter below details

Server Name in General Tab

In Connection tab

  1. Host Name – localhost ( or hostname of your database server )
  2. Port – 5432 (default)
  3. Maintenance Database – postgres
  4. Username – postgres
  5. Password – add your password

Click Save

Click on Servers and chose the server name you have saved, chose the database and open the sql editor and start writing sql on PostgreSQL

Method 2 : Using brew install

For complete installation steps please refer below details taken from Github

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 readlineONLY 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

Github Link – https://gist.github.com/ibraheem4/ce5ccd3e4d7a65589ce84f2a3b7c23a3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: