---
title: 'Setting up a local PostgreSQL database'
metaTitle: 'How to Run and Setup a Local PostgreSQL Database | Prisma'
metaDescription: 'Learn how to set up a PostgreSQL database on Windows, macOS, and Linux by following the step-by-step instructions in this practical guide.'
metaImage: '/social/generic-postgresql.png'
authors: ['justinellingwood']
toc: false
---

This page explains how to install and configure a [PostgreSQL database server](https://www.postgresql.org/docs/current/intro-whatis.html) and the [`psql` command line client](https://www.postgresql.org/docs/current/app-psql.html). This guide will cover how to install and set up these components on your computer for local access.

This guide will cover the following platforms:

- [Setting up PostgreSQL on Windows](#setting-up-postgresql-on-windows)
- [Setting up PostgreSQL on macOS](#setting-up-postgresql-on-macos)
- [Setting up PostgreSQL on Linux](#setting-up-postgresql-on-linux)
  - [Debian and Ubuntu](#debian-and-ubuntu)
  - [CentOS and Fedora](#centos-and-fedora)

Navigate to the sections that match the platforms you will be working with.

## Setting up PostgreSQL on Windows

The PostgreSQL project provides a native Windows installer to install and configure your database.

Visit the [PostgreSQL Windows installation page](https://www.postgresql.org/download/windows/) to find a link to the installer. Click **Download the installer** at the start of the page:

![PostgreSQL initial download page](/img/orm/dataguide/setting-up-a-local-database/windows/initial_download_page.png)

On the page that follows, in the Windows x86-64 or Windows x86-32 columns (depending on your computer's architecture), choose the PostgreSQL version you'd like to install:

![PostgreSQL available versions](/img/orm/dataguide/setting-up-a-local-database/windows/installer_download.png)

Click **Download** on your chosen version and save the file to a convenient location.

Once the download completes, double click on the file to run the installer (you may have to confirm that you wish to allow the program to make changes to your computer):

![PostgreSQL installer greeting](/img/orm/dataguide/setting-up-a-local-database/windows/installer_greeting.png)

Click **Next** on the initial page to begin configuring your installation.

On the next page, choose your installation directory:

![PostgreSQL installation directory](/img/orm/dataguide/setting-up-a-local-database/windows/installation_directory.png)

Click **Next** to accept the default location.

The next page allows you to choose which components you wish to install. You need the **PostgreSQL Server** and **Command Line Tools** selected at a minimum:

![PostgreSQL choose components](/img/orm/dataguide/setting-up-a-local-database/windows/choose_components.png)

Click **Next** to continue.

Now, choose the location where the database data files will be stored:

![PostgreSQL data directory](/img/orm/dataguide/setting-up-a-local-database/windows/data_directory.png)

Click **Next** to accept the default location.

Next, select and confirm an administrative password for the PostgreSQL superuser (called `postgres`):

![PostgreSQL set password](/img/orm/dataguide/setting-up-a-local-database/windows/set_password.png)

Click **Next** when you are finished.

Choose the port that the server will listen to:

![PostgreSQL set port](/img/orm/dataguide/setting-up-a-local-database/windows/set_port.png)

Click **Next** to accept the default 5432 port.

Now, pick the locale that your database will use:

![PostgreSQL locale](/img/orm/dataguide/setting-up-a-local-database/windows/set_locale.png)

Click **Next** to use the default locale of your computer.

The configuration portion of the installation is now complete. You can review a summary of the choices you've made:

![PostgreSQL installation summary](/img/orm/dataguide/setting-up-a-local-database/windows/installation_summary.png)

If everything looks correct, click **Next**.

Finally, PostgreSQL is ready to be installed:

![PostgreSQL ready to install](/img/orm/dataguide/setting-up-a-local-database/windows/ready_to_install.png)

Click **Next** to begin the installation process.

Once the installation is complete, you can verify the installation using the `psql` command line tool.

In your start menu, type `psql` and click on the tool to launch the program. You will be prompted to enter the connection details that you wish to use.

![Running psql client](/img/orm/dataguide/setting-up-a-local-database/windows/psql.png)

Press **Enter** to accept the default choices given in the square brackets. The final prompt will be for the password for the `postgres` user that you configured during setup.

Upon successfully authenticating, you will be dropped into an interactive `psql` session with your database.

When you are finished, exit the session by typing:

```
\quit
```

## Setting up PostgreSQL on macOS

The PostgreSQL project provides a native macOS installer to install and configure your database.

Visit the [PostgreSQL macOS installation page](https://www.postgresql.org/download/macosx/) to find a link to the installer. Click **Download the installer** at the start of the **Interactive Installer by EnterpriseDB** section:

![PostgreSQL initial download page](/img/orm/dataguide/setting-up-a-local-database/macos/initial_download_page.png)

On the page that follows, in the Mac OS X column, choose the PostgreSQL version you'd like to install:

![PostgreSQL available versions](/img/orm/dataguide/setting-up-a-local-database/macos/installer_download.png)

Click **Download** on your chosen version and save the file to a convenient location.

Once the download completes, find the PostgreSQL installer DMG in the Downloads folder. Double click on the downloaded DMG file to mount the installer archive:

![PostgreSQL DMG file](/img/orm/dataguide/setting-up-a-local-database/macos/dmg_file.png)

In the mounted archive, click on the PostgreSQL installer package (you may be prompted to confirm that you want the package to be able to make changes to your computer):

![PostgreSQL installer package](/img/orm/dataguide/setting-up-a-local-database/macos/installer_package.png)

The PostgreSQL installer will open to the welcome screen:

![PostgreSQL installer welcome](/img/orm/dataguide/setting-up-a-local-database/macos/welcome.png)

Click **Next** on the initial page to begin configuring your installation.

On the next page, choose your installation directory:

![PostgreSQL installation directory](/img/orm/dataguide/setting-up-a-local-database/macos/installation_directory.png)

Click **Next** to accept the default location.

The next page allows you to choose which components you wish to install. You need the **PostgreSQL Server** and **Command Line Tools** selected at a minimum:

![PostgreSQL choose components](/img/orm/dataguide/setting-up-a-local-database/macos/choose_components.png)

Click **Next** to continue.

Now, choose the location where the database data files will be stored:

![PostgreSQL data directory](/img/orm/dataguide/setting-up-a-local-database/macos/choose_data_directory.png)

Click **Next** to accept the default location.

On the next page, select and confirm an administrative password for the PostgreSQL superuser (called `postgres`):

![PostgreSQL set password](/img/orm/dataguide/setting-up-a-local-database/macos/set_password.png)

Click **Next** when you are finished.

The following page lets you choose the port that the server will listen to:

![PostgreSQL set port](/img/orm/dataguide/setting-up-a-local-database/macos/choose_port.png)

Click **Next** to accept the default 5432 port.

Now, pick the locale that your database will use:

![PostgreSQL locale](/img/orm/dataguide/setting-up-a-local-database/macos/choose_locale.png)

Click **Next** to use the default locale of your computer.

The configuration portion of the installation is now complete. You can review a summary of the choices you've made:

![PostgreSQL installation summary](/img/orm/dataguide/setting-up-a-local-database/macos/installation_summary.png)

If everything looks correct, click **Next**.

PostgreSQL is ready to be installed:

![PostgreSQL ready to install](/img/orm/dataguide/setting-up-a-local-database/macos/ready_to_install.png)

Click **Next** to begin the installation process.

The installer will confirm completion when the process completes:

![PostgreSQL installation complete](/img/orm/dataguide/setting-up-a-local-database/macos/installation_complete.png)

Now that PostgreSQL is installed, you can verify the installation using the `psql` command line tool. While this client is installed, we need to modify our terminal's `PATH` variable to access it easily.

Open a new terminal window to begin. First, find the PostgreSQL `bin` directory by typing:

```
ls -d /Library/PostgreSQL/*/bin
```

The response will be the directory of your PostgreSQL `bin` directory. For example:

```
/Library/PostgreSQL/12/bin
```

Now that you know the directory to use, edit the `/etc/paths` file by typing:

```
sudo nano /etc/paths
```

You will be prompted for your computer's administrative password to continue:

![PostgreSQL open paths file](/img/orm/dataguide/setting-up-a-local-database/macos/open_paths_file.png)

Add the path to `bin` directory that you found to the bottom of the file:

![PostgreSQL edit paths file](/img/orm/dataguide/setting-up-a-local-database/macos/edit_paths_file.png)

When you are finished, save and close the file by typing <kbd>CTL-X</kbd>, <kbd>Y</kbd>, and hitting <kbd>ENTER</kbd>.

To use the new `PATH` settings, open a new terminal window. In the new window, type:

```
psql -U postgres
```

You will be prompted for the `postgres` user's administrative password that you set during the PostgreSQL configuration process:

![PostgreSQL psql authentication](/img/orm/dataguide/setting-up-a-local-database/macos/psql_authentication.png)

Upon successfully authenticating, you will be dropped into an interactive `psql` session with your database.

To exit the session when you are finished, type:

```
\quit
```

## Setting up PostgreSQL on Linux

Installation methods differ depending on the Linux distribution you are using. Follow the section below that matches your Linux distribution.

- [Debian and Ubuntu](#debian-and-ubuntu)
  - [Install using Debian or Ubuntu's default repositories](#install-using-debian-or-ubuntus-default-repositories)
  - [Install using the PostgreSQL project's Debian and Ubuntu repositories](#install-using-the-postgresql-projects-debian-and-ubuntu-repositories)
- [CentOS and Fedora](#centos-and-fedora)
  - [Install using CentOS or Fedora's default repositories](#install-using-centos-or-fedoras-default-repositories)
  - [Install using the PostgreSQL project's CentOS and Fedora repositories](#install-using-the-postgresql-projects-centos-and-fedora-repositories)

### Debian and Ubuntu

You can either choose to use the version of PostgreSQL available in your distribution's default repositories or use repositories provided by the PostgreSQL project. Packages in the default repository are tested to work with all other software provided for your distribution, but may be older. Packages from the PostgreSQL project will be more up-to-date but may require extra configuration.

- [Install using Debian or Ubuntu's default repositories](#install-using-debian-or-ubuntus-default-repositories)
- [Install using the PostgreSQL project's Debian and Ubuntu repositories](#install-using-the-postgresql-projects-debian-and-ubuntu-repositories)

#### Install using Debian or Ubuntu's default repositories

Both Ubuntu and Debian provide versions of PostgreSQL server as packages within their default repositories. The PostgreSQL version may be older than those found on the PostgreSQL website, but this is the simplest way to install on these distributions.

To install PostgreSQL server, update your computer's local package cache with the latest set of packages. Afterwards, install the `postgresql` package:

```
sudo apt update
sudo apt install postgresql
```

By default, PostgreSQL is configured to use [peer authentication](https://www.postgresql.org/docs/10/auth-methods.html#AUTH-PEER), which allows users to log in if their operating system user name matches a PostgreSQL internal name.

The installation process created an operating system user called `postgres` to match the `postgres` database administrative account. To log into PostgreSQL with the `psql` client, use `sudo` to run the command as the `postgres` user:

```
sudo -u postgres psql
```

When you are finished, you can exit the `psql` session by typing:

```
\quit
```

#### Install using the PostgreSQL project's Debian and Ubuntu repositories

If you need a more up-to-date version of PostgreSQL, you can use the repositories maintained by the PostgreSQL project instead of those provided by your Linux distribution.

First, add a new repository definition to your system by typing:

```
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(. /etc/os-release; echo $VERSION_CODENAME)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
```

Next, add the PostgreSQL package signing key to `apt` so that it trusts the packages in the new repository:

```
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
```

With the repository set up, you can update your local package index and then install PostgreSQL by typing:

```
sudo apt update
sudo apt install postgresql
```

By default, PostgreSQL is configured to use [peer authentication](https://www.postgresql.org/docs/10/auth-methods.html#AUTH-PEER), which allows users to log in if their operating system user name matches a PostgreSQL internal name.

The installation process created an operating system user called `postgres` to match the `postgres` database administrative account. To log into PostgreSQL with the `psql` client, use `sudo` to run the command as the `postgres` user:

```
sudo -u postgres psql
```

When you are finished, you can exit the `psql` session by typing:

```
\quit
```

### CentOS and Fedora

You can either choose to use the version of PostgreSQL available in your distribution's default repositories or use repositories provided by the PostgreSQL project. Packages in the default repository are tested to work with all other software provided for your distribution, but may be older. Packages from the PostgreSQL project will be more up-to-date but may require extra configuration.

- [Install using CentOS or Fedora's default repositories](#install-using-centos-or-fedoras-default-repositories)
- [Install using the PostgreSQL project's CentOS and Fedora repositories](#install-using-the-postgresql-projects-centos-and-fedora-repositories)

#### Install using CentOS or Fedora's default repositories

Both CentOS and Fedora provide versions of PostgreSQL server as packages within their default repositories. The PostgreSQL version may be older than those found on the PostgreSQL website, but this is the simplest way to install on these distributions.

To install PostgreSQL server, use your distribution's package manager to install the `mysql-server` package:

For **CentOS** use the `yum` package manager:

```
sudo yum install postgresql-server
```

For **Fedora** use the `dnf` package manager:

```
sudo dnf install postgresql-server
```

Once the MySQL package is installed, initialize the database by typing:

```
sudo postgresql-setup initdb
```

Afterwards, start up the service by typing:

```
sudo systemctl start postgresql.service
```

Optionally, you can automatically start PostgreSQL on boot by typing:

```
sudo systemctl enable postgresql.service
```

By default, PostgreSQL is configured to use [peer authentication](https://www.postgresql.org/docs/10/auth-methods.html#AUTH-PEER), which allows users to log in if their operating system user name matches a PostgreSQL internal name.

The installation process created an operating system user called `postgres` to match the `postgres` database administrative account. To log into PostgreSQL with the `psql` client, use `sudo` to run the command as the `postgres` user:

```
sudo -u postgres psql
```

When you are finished, you can exit the `psql` session by typing:

```
\quit
```

#### Install using the PostgreSQL project's CentOS and Fedora repositories

If you need a more up-to-date version of PostgreSQL, you can use the repositories maintained by the PostgreSQL project instead of those provided by your Linux distribution.

To configure the PostgreSQL project's repository, download and install the repository setup package with in your terminal.

For **CentOS** execute the following command:

```
sudo yum install "https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(. /etc/os-release; echo $VERSION_ID)-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
```

For **Fedora** use this command:

```
sudo dnf install "https://download.postgresql.org/pub/repos/yum/reporpms/F-$(. /etc/os-release; echo $VERSION_ID)-x86_64/pgdg-fedora-repo-latest.noarch.rpm"
```

If you are using **CentOS 8**, you must also disable the system's PostgreSQL module to prevent it from interfering with the repository's PostgreSQL version. To do so, type:

```
sudo yum module disable postgresql
```

Next, choose the version of PostgreSQL that you wish to target.

For **CentOS**, check which versions of PostgreSQL are available by typing:

```
yum list postgresql*-server
```

For **Fedora** use the `dnf` package manager:

```
dnf list postgresql*-server
```

After deciding which version to use, you can install it using your package manager.

For **CentOS** use the `yum` package manager. For example, to install PostgreSQL 12, type:

```
sudo yum install postgresql12-server
```

For **Fedora** use the `dnf` package manager. For example, to install PostgreSQL 12, type:

```
sudo dnf install postgresql12-server
```

Once the PostgreSQL package is installed, initialize the database:

```
sudo /usr/pgsql-*/bin/postgresql-*-setup initdb
```

Find the name of the systemd unit file for your version of PostgreSQL:

```
systemctl list-unit-files | grep postgresql
```

Start up the service using the unit file you found. For example, for PostgreSQL 12, it would be:

```
sudo systemctl start postgresql-12.service
```

Optionally, you can also automatically start PostgreSQL on boot with the `enable`:

```
sudo systemctl enable postgresql-12.service
```

By default, PostgreSQL is configured to use [peer authentication](https://www.postgresql.org/docs/10/auth-methods.html#AUTH-PEER), which allows users to log in if their operating system user name matches a PostgreSQL internal name.

The installation process created an operating system user called `postgres` to match the `postgres` database administrative account. To log into PostgreSQL with the `psql` client, use `sudo` to run the command as the `postgres` user:

```
sudo -u postgres psql
```

When you are finished, you can exit the `psql` session by typing:

```
\quit
```
