Getting Started with PostgreSQL on Mac OSX _ Codementor
Getting Started with PostgreSQL on Mac OSX _ Codementor
Sign up
Table of contents
I. Introduction
II. About PostgreSQL
III. Getting Started
3. Configuring Postgres
IV. Popular GUIs for PostgreSQL on MacOSX
V. Summary
VI. References
I. Introduction
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 1/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
The other day I began a new Node.js project and wanted to use PostgreSQL
as my database backend. If you’re here reading this tutorial, hopefully, you
are trying to do the same!
This tutorial will teach you how to set up, configure, and use PostgreSQL on
MacOSX 10.7 (Lion) and above. You will need at least a basic level of comfort
using the command line using either the MacOSX built-in terminal, iTerm2,
Zsh, or something similar. By the time we are finished, you will:
Streaming replication
Schemas
User-defined objects like operators, data types, and functions
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 2/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Nested transactions
Table inheritance
Partitioning
Several unusual data types, like Money, Geometry, IP addresses, JSON,
and data ranges.
Can execute stored procedures in over a dozen programming
languages, including Java, Perl, Python, Ruby, and C/C++.
Due to its first-class support for JSON, Postgres is often a good alternative to
“No-SQL” databases like MongoDB. Read more about PostgreSQL
You can pick whichever option is right for you. For this tutorial, let’s see how
to install on the command line.
1. Getting Homebrew
To install PostgreSQL on the command line we will be using a package
manager called Homebrew. If you already have Homebrew installed, you
can skip to Installing PostgreSQL. Otherwise, let’s keep going.
Let’s start by copying and pasting the following command into our
command line:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 3/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Don’t worry about what all that does—as long as you’re using MacOSX 10.7
or higher, the Ruby interpreter comes pre-installed. The rest of this
command (the curl command) is a tool to make web requests from your
command line. This one downloads the script at the given URL, and then the
Ruby interpreter executes it. You should see output similar to the following:
Installing Homebrew
2. Installing Postgres
Now that we have Homebrew, installing PostgreSQL is very easy. Run the
following command to install PostgreSQL using Homebrew:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 4/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 5/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
*Installing *
Starting Services
Let’s go ahead and start Postgres running, and make sure Postgres starts
every time your computer starts up. Execute the following command:
Finally, let’s make sure Postgres is installed and running. Let’s check what
version is running:
postgres -V
Postgres Installed
That’s it! You have Postgres installed. Now we’re finally ready to learn how to
use it!
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 6/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
3. Configuring Postgres
Postgres works pretty hard to make itself usable right out of the box
without you having to do anything. By default, it automatically creates the
user postgres . Let’s see what other users it has created. Let’s start by using
the psql utility, which is a utility installed with Postgres that lets you carry
out administrative functions without needing to know their actual SQL
commands.
psql postgres
(You may need to use sudo psql postgres for this command to work,
depending on how your system is configured).
psql output
That’s the psql command line. We can now enter a command to see what
users are installed:
postgres=# \du
Under the covers, this command executes an SQL query (we’ll learn about
those later) that gets all the users in the database. On my machine, it
returns the following:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 7/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
We see the Postgres user I mentioned, but what is that other user,
engineerapart ? This is one of the things Postgres does to make your life
easier when you first install it. On most Linux-based operating systems, the
username that is used by default by all processes is the one you are logged
in as. You don’t have to pass your username to most programs. But if a
particular program, like Postgres, doesn’t have your username configured—
it will fail!
A. Creating Users
Postgres doesn’t actually directly manage users or groups, like most
standard permission models do. Instead, it directly manages what it calls
roles.
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 8/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Use the createuser utility that comes installed with Postgres (which is
just a wrapper for executing CREATE ROLE ).
Where username is the user you want to create, and the password goes at
the end in quotes. We will get to the options later.
psql output
While we’re in here, let’s set the password for the default postgres account
—by default, it has no password.
You will be prompted to enter the password and confirm it. Now let’s create
our new role:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 9/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Wait. The attributes list for the user patrick is completely empty. Why?
This is how Postgres securely manages defaults. This user can read any
database, table, or row it has permissions for, but nothing else—it cannot
create or manage databases and has no admin powers. This is a good thing!
It helps keep your database secure.
So let’s add the CREATEDB permission to our new user to allow them to
create databases:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 10/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 11/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
So let’s use createuser to do the same thing we did above: create the
patrick user:
createuser patrick
This creates the user patrick with all of the default attributes, again,
without the ability to create databases. If we wanted the ability to create a
database, you would execute the following instead:
A.3. Summary
That’s it! Now our new user is set up and can create databases. Let’s start
managing our database with that new user.
B. Creating a Database
Just like creating a user, there are two ways to create a database:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 12/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
You’ll notice the prompt is slightly different – the # has changed to a > .
This indicates you’re no longer using a Super User account.
Once this is done, you need to add at least one user who has permission to
access the database (aside from the super users, who can access
everything). To do that, we’re going to learn a new SQL command:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 13/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Here, I have also shown you a few new commands that can be used with
psql :
You can now create, read, update and delete data on our
super_awesome_application database with the user patrick !
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 14/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Here, I have invoked the createdb utility and passed it the patrick user to
use for connecting to the database. It is that user whose permissions will be
checked to execute the create command.
It is very, very rare that you will want to change a database after it is
created. The only things you can change are its name and some
configuration parameters. Changing configuration parameters (such as
collation or character sets) have implications that go far outside of this
tutorial. However, we can change the name if we’d like.
Again, there is no command line tool to change a database once it’s created.
We must use psql :
B.3. Summary
That’s it! We have created our user, created a database, and given the user
permissions to use the database. Along the way, we learned to use some of
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 15/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
the pre-installed Postgres command line tools. This will give us a great
foundation for understanding more complex behavior in the next section.
Now let’s take a look at some popular graphical tools for managing
PostgreSQL on MacOSX.
1. Postico (https://eggerapps.at/postico/)
Postico is a modern Postgres client for OSX, built by the same developer
who built Postgres.app (mentioned above). It is free, but you can buy a
license to unlock additional power features. This is the GUI that I use to
manage Postgres because it is built specifically for Mac and has a beautiful,
very easy to use (but powerful) UI. It also includes an SQL editor for complex
queries.
Download it at https://eggerapps.at/postico/download/
Double-click the downloaded Zip file in Finder
Drag the extracted Postico.app file to your Applications folder
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 16/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
You’ll see a screen that looks like the following (without any database
connections configured):
Postico
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 17/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Postico connected
2. pgAdmin (https://www.pgadmin.org/)
pgAdmin is the oldest of the Postgres GUIs, its first version being released
just a few months after Postgre’s first release in 1996. Having been rewritten
several times, it can run on Linux, MacOSX, and Windows, and features
powerful database management tools including a syntax-highlighted SQL
editor. Designed to run on both client machines and on deployed servers,
pgAdmin is capable of handling advanced cases that Postico cannot.
Download it at https://www.pgadmin.org/download/macos4.php
Double-click the downloaded disc image (DMG) file in your Downloads
folder
Drag the pgAdmin 4 app to your Applications folder
Find pgAdmin in Launchpad and launch the app.
pgAdmin
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 19/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
pgAdmin connected
3. Navicat (https://www.navicat.com/products/navicat-for-
postgresql)
Navicat is without a doubt one of the best Enterprise-level management
GUIs for PostgreSQL. Featuring not only an SQL editor and database, table
and row viewing and management, Navicat also features a Data Modeler
that allows you to graphically design and debug your database schemas.
However, although Navicat comes with a 14-day free trial, its licenses range
from $100-$250.
Download it at https://www.navicat.com/download/navicat-for-
postgresql
Double-click the downloaded disc image (navicat112pgsqlen.dmg) file
in your Downloads folder
Drag the Navicat app to your Applications folder
Find Navicat in Launchpad and launch the app.
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 20/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Navicat
Click “Connection”
Fill out the dialog provided using the same information as above
Click “Ok” to save the connection.
Double-click the new connection in the list on the left, and you’re
connected. That’s it!
Navicat connect
Read the Navicat Online Manual to learn how to use Postico’s amazing
features!
V. Summary
PostgreSQL is one of the top relational databases in the world, used by
individuals, small businesses, and huge enterprises alike. Despite its high
performance and powerful feature set, it is incredibly easy to get started
with it. In this tutorial, we have learned how to:
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 21/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
VI. References
PostgreSQL Homepage
PostgreSQL Documentation
Postico Documentation
pgAdmin Documentation
Navicat Online Manual
DevOps Report
112 48
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 22/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
Patrick Sears
Experienced and passionate developer of web applications
Patrick Sears is a principal engineer with over 2 decades of experience FOLLOW
with HTML, CSS and Javascript. Although he got his start 15 years ago
writing C for digital signal processors, his current passion is creating
amazing user ex...
48 Replies
P.S. I hope this won’t be considered as advertising that I leave the link.
Show more
If it is, please forgive me :-)
Just saved my life, Patrick! Thank you so much for such a complete and
detailed amazing content :)
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 23/24
03/12/2024, 15:23 Getting Started with PostgreSQL on Mac OSX | Codementor
GET STARTED
https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb 24/24