/

How to Install PostgreSQL on macOS

How to Install PostgreSQL on macOS

Learn how to install the PostgreSQL Database Management System (DBMS) on macOS using the following instructions.

If you’re using Windows or Linux, visit https://www.postgresql.org/download/ to choose your package. You can also search for instructions specific to your platform, such as “how to install postgres on Windows” or “how to install postgres on [your Linux distribution].”

The installation process is similar across platforms, especially after the initial installation phase.

On macOS, we will use Homebrew. If you haven’t installed Homebrew yet, go to https://brew.sh/ and follow the provided instructions. Once installed, run the following commands in your terminal:

1
2
brew install postgresql
brew services start postgresql

This will install PostgreSQL and start it as a daemon, meaning it will run in the background and listen for connections.

Using Homebrew has the advantage of easy updates. You can update PostgreSQL by running the following commands:

1
2
3
brew upgrade postgresql
brew postgresql-upgrade-database
brew services restart postgresql

Note that postgresql is just an alternate name for PostgreSQL; they refer to the same thing. The name incorporates “SQL,” which stands for Structured Query Language. SQL is a special language used to interact with relational databases.

Now that PostgreSQL is installed, let’s log in. In your terminal, type:

1
psql postgres

This gives you access to the default postgres database, which was created during installation using your macOS username.

With the psql application running, you can create a new database by executing the following command:

1
CREATE DATABASE test;

Remember to include the semicolon at the end of the command; otherwise, it will not run.

To switch to the newly created database, use the command:

1
\c test

The prompt will confirm that you are connected to the “test” database as your user.

Next, let’s create a new table. Use the following syntax:

1
2
3
4
5
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);

If the table is created without any errors, you can verify its existence by running the command:

1
\dt

This command will display the database tables. If you make any mistakes, you can delete the table using:

1
DROP TABLE users

To exit the psql application, type:

1
\q

Alternatively, you can type quit.

Now that you’re familiar with the underlying concepts, let’s explore an easier way to work with PostgreSQL and other databases: the TablePlus app. This app is compatible with macOS, Windows, and Linux.

  1. Connect to your database by specifying the “test” database name.
  2. Use the graphical user interface to inspect tables.
  3. Execute SQL queries easily within the app.

With TablePlus, managing and interacting with your PostgreSQL database becomes much simpler.

tags: [“PostgreSQL”, “macOS”, “Homebrew”, “SQL”, “relational database”, “database management”]