How to install PostgreSQL on macOS

Instructions for installing PostgreSQL DBMS

The following instructions for installing PostgreSQL are based on macOS.

For Windows and Linux, go tohttps://www.postgresql.org/download/And select your package.

Also search for "How to install Postgres on Windows" or "How to install postgres on Windows".your linux distribution"(If you are using another platform).

It should be the same, especially after the installation phase.

On macOS, we will useHouse wine. If Homebrew is not installed yet, go tohttps://brew.sh/And follow the instructions there.

When finished, go back and run in the command line:

brew install postgresql

And when it's finished, run:

brew services start postgresql

TobeginPostgreSQL acts as a daemon, which means it will continue to run in the background to monitor connections.

The huge advantage of using Homebrew is that you can install any updates by running the following command

brew upgrade postgresql
brew postgresql-upgrade-database
brew services restart postgresql

postgresqlThe name of PostgreSQL is more complicated, but they are the same thing. It just embeds SQL in the name. what isSQL? SQL (called "sequel") stands for Structured Query Language, it is a special language that we use to communicate withRelational Database.

If you are new to databases, this is a lot of new terms for you! Basically, relational databases organize data intotable, And provides a way to insert and extract data from these tables. That is SQL.

We will use it soon.

After we log in to PostgreSQL!

Return to the command line and type

psql postgres

using psql

This will enable you to accesspostgresThe database created with your macOS username is used by default. Homebrew will automatically create your user during installation.

Now we enterpsqlApplication, we can create a new database:

CREATE DATABASE test;

Don't forget the semicolon;, Because SQL requires it, otherwise the command will not run.

Now in the new line, we use the following command to switch to that database

\c test

The prompt will display the following:

You are now connected to database "test" as user "flaviocopes"..

Now we will 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
);

Now, if you run this command and no errors are displayed, the table will be included in the system.

You can view it by running the command

\dt

This will show you the database tables:

If any errors occur, you can delete the table by running the following command

DROP TABLE users

Finally exitpsql, Run

\q

Or just typequit.

Now that you know the behind-the-scenes knowledge of things, I will show you an easier way to use PostgreSQL and other databases:TablePlus application. It is suitable for macOS, Windows and Linux.

Connect to the database:

SpecifytestName database:

In addition to being able to use the graphical user interface checklist:

It also allows us to use SQL queries very easily:


More database tutorials: