PostgreSQL and PostGIS installation in Mac OS.

PostGIS is spatial database extender for PostgreSQL object-relational database. It adds supports for geographic objects allowing location queries to be run in SQL.

When I was setting up a Rails project in my local machine it requires PostGIS setup to run the migration. I have went through their official sites and many other document to know about the installation process and found out these processes, to reinstall the PostgreSQL if another version of PostgreSQL is installed previously and installing PostGIS in Mac OS.

Remove previous versions of PostgreSQL

brew uninstall --force postgresql

Delete all Files of Postgres

rm -rf /usr/local/var/postgres

Install Postgres with Homebrew

brew install postgres

Install PostGIS with Homebrew

brew install postgis

Start PostgreSQL server

pg_ctl -D /usr/local/var/postgres start

Create Database

initdb /usr/local/var/postgres

If terminal shows an error

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

Remove old database file

rm -r /usr/local/var/postgres

Run the initdb command again

initdb /usr/local/var/postgres

Create a new database

createdb postgis_test

Enable PostGIS

psql postgis_test

This command should show the psql command prompt

psql (10.0)
type “help” for help
postgis_test=#

Creating extension for PostGIS.

CREATE EXTENSION postgis;

If everything goes well we should see:

postgis_test=#CREATE EXTENSION postgis;
CREATE EXTENSION
postgis_test=#

Check your PostGIS version

SELECT PostGIS_Version();

Hopefully this article will save your time .