Learn how to install PostgreSQL 14 on macOS with Homebrew or the official installer, including configuration and verification
In this guide, you will learn how to set up PostgreSQL 14.7 on your MacBook using Homebrew, a widely-used package manager for macOS. The steps are straightforward and include:
Ensure you have a MacBook or iMac running macOS 10.13 or later.
Install Xcode Command Line Tools To start, you need to install the Xcode Command Line Tools, which are essential for software development on your Mac.
xcode-select --installA pop-up window will prompt you to install the Command Line Tools. Click “Install” to proceed. After installation is complete, move on to the next step.
With the Xcode Command Line Tools installed, you can now install Homebrew. Homebrew is a package manager for macOS that simplifies the installation of software packages like PostgreSQL.
After installing Homebrew, it’s important to add it to your system’s PATH. This tells the system where to find the Homebrew executables, allowing you to run Homebrew commands from any location in your Terminal.
1. Install Homebrew Copy and paste the following command into the Terminal app. The script will automatically download and install Homebrew on your Mac. You might be asked to enter your admin password during the installation process.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"After the installation, you will see a message saying, “Installation successful!”
2. Add Homebrew to Your PATH To complete the installation, add Homebrew to your PATH:
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/your_username/.zprofileReplace /Users/your_username with your actual user directory.
3. Load Homebrew Environment Variables Run the following command to load the Homebrew environment variables into your current Terminal session:
eval "$(/opt/homebrew/bin/brew shellenv)"4. Verify the Installation To confirm Homebrew is installed correctly, run:
brew --versionThis should display the Homebrew version number.
In this step, you will use Homebrew to install PostgreSQL on your macOS system.
1. Ensure Homebrew is Installed Make sure you have Homebrew installed. If not, refer to the previous section to install it.
2. Update Homebrew Update Homebrew to ensure you have the latest package information:
brew update3. Install PostgreSQL 14 Install PostgreSQL 14 using Homebrew:
brew install postgresql@144. Start PostgreSQL Service After installation, start the PostgreSQL service:
brew services start postgresql@14You should see a message indicating that PostgreSQL has started successfully.
5. Verify PostgreSQL Installation Check the PostgreSQL version to ensure it’s installed correctly:
psql --version6. Create a PostgreSQL User
Create a new PostgreSQL user named postgres with superuser privileges:
createuser -s postgres7. Set a Password for the postgres User
Set a password for the postgres user:
psql
\password postgres8. Configure PostgreSQL Environment
Add PostgreSQL to your PATH by adding the following line to your shell profile (~/.zshrc or ~/.bash_profile):
export PATH="/usr/local/opt/postgresql@14/bin:$PATH"Then, reload your profile:
source ~/.zshrc # or source ~/.bash_profile9. Troubleshooting Tips If the PostgreSQL service does not start, check the logs for errors:
tail -f /usr/local/var/log/postgres.logEnsure your system has enough resources and dependencies for PostgreSQL.
The Northwind database is a sample database originally provided by Microsoft. It contains data on customers, orders, products, suppliers, and other aspects of a fictitious company named “Northwind Traders”. We will use a version of Northwind adapted for PostgreSQL.
1. Open Terminal 2. Create a New Directory Create a new directory for the Northwind database and navigate to it:
mkdir northwind && cd northwind3. Download the SQL File Download the Northwind PostgreSQL SQL file using curl:
curl -O https://raw.githubusercontent.com/pthom/northwind_psql/master/northwind.sqlNow that you have downloaded the Northwind SQL file, it’s time to create a new database on your PostgreSQL server.
1. Connect to PostgreSQL Server
Connect as the postgres user:
psql -U postgres2. Create Database
Create a new database called northwind:
CREATE DATABASE northwind;3. Exit PostgreSQL Interface
Exit the psql command-line interface:
\qImport the Northwind SQL file into the northwind database.
1. Ensure You’re in the Correct Directory
Ensure you’re in the northwind directory where the northwind.sql file is located.
2. Import the SQL File
Run the following command to import the Northwind SQL file into the northwind database:
psql -U postgres -d northwind -f northwind.sqlVerify that the Northwind database has been installed correctly.
1. Connect to the northwind Database
Connect using psql:
psql -U postgres -d northwind2. List Tables List the tables in the Northwind database:
\dtYou should see a list of Northwind tables.
3. Run a Sample Query
Query the customers table to ensure data has been imported correctly:
SELECT * FROM customers LIMIT 5;4. Exit the psql Interface
Exit the command-line interface:
\qCongratulations! You have successfully installed the Northwind database in PostgreSQL using an SQL file and psql.
Jupyter Notebook is a great tool for executing SQL queries and analyzing the Northwind database.
1. Install ipython-sql
Install the ipython-sql package:
!pip install ipython-sql2. Load SQL Extension Load the SQL extension:
%load_ext sql3. Connect to the Northwind Database Establish a connection:
%sql postgresql://postgres@localhost:5432/northwindOn Windows, you may need to include the password:
%sql postgresql://postgres:{password}@localhost:5432/northwind4. Run the Cell
Run the cell by either clicking the “Run” button or using the keyboard shortcut Shift + Enter or Ctrl + Enter.
5. Verify Connection You should see an output confirming the connection:
'Connected: postgres@northwind'Based on what you’ve accomplished, here are some next steps to continue your learning journey:
psycopg2, a popular PostgreSQL adapter for Python.SQLAlchemy to manage your database using Python.This blog post provides a comprehensive guide to installing PostgreSQL 14.7 on macOS using Homebrew, importing the Northwind database, and connecting to it using Jupyter Notebook.