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:
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:
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:
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:
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:
\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:
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:
\dt
This command will display the database tables. If you make any mistakes, you can delete the table using:
DROP TABLE users
To exit the psql
application, type:
\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.
- Connect to your database by specifying the “test” database name.
- Use the graphical user interface to inspect tables.
- Execute SQL queries easily within the app.
With TablePlus, managing and interacting with your PostgreSQL database becomes much simpler.