How to Install PostgreSQL on Ubuntu, Linux & Mac
Tags: ubuntu, postgres, linux, mac
Published: 2020-05-23 22:21:09

---
title: How to Install PostgreSQL on Ubuntu, Linux & Mac
published: true
date: 2020-05-08 09:50:19 UTC
tags: ubuntu,postgresql,linux,mac
canonical_url: https://medium.com/@Josylad/how-to-install-postgresql-on-ubuntu-linux-mac-5e08b09b3fb9
---

So, recently I had to reinstall my OS and I found myself struggling with setting up my development environment which includes PostgreSQL on the new installation and as you might have known, I work a lot with PostgreSQL and I had to install it before I could do anything.

fortunately, I was able to set up everything **perfectly** and get my Django projects running within a short time.

SQL Databases like PostgreSQL store data in tables which are different items in an application. Tables have a fixed number of columns and a variable number of rows.

![](https://cdn-images-1.medium.com/max/800/1*PY24xlr4TpOkXW04HUoqrQ.jpeg)

Below is a quick guide that should help anyone running on a Linux to get their PostgreSQL installation working with no hassles.

**Please follow the steps carefully in order to avoid any error!**

### Installation

#### 1. Ubuntu

To Install Postgres on Ubuntu we need to run the following commands in our terminal.

```
$ sudo apt-get update
```

To install Postgres

```
$ sudo apt-get install postgresql postgresql-contrib libpq-dev
```

Enter **y** when prompted “Do you want to continue? **[Y/n]**” and wait as the installation completes.

#### Defining a user role

Postgres uses “roles” to aid in authentication and authorization. By default, Postgres creates a Postgres user and is the only user who can connect to the server.

We want to create our own superuser role to connect to the server.

For those running on elementary or parrot, run the following command first;

```
$ sudo service postgresql start

$ sudo -u postgres createuser --superuser $USER
```

Enter your desired password when prompted.

We then have to create a database with that $USER login name, this is what Postgres expects as default.

```
$ sudo -u postgres createdb $USER
```

Navigate to your home directory and enter the following command to create the .psql\_history in order to save your history:

```
$ touch .psql\_history
```

You can now connect to the Postgres server by typing :

```
$ psql
```

#### 2. Mac

Homebrew makes it really easy to install Postgres. Just run:

```
$ brew install postgres
```

After it finishes installing, you’ll need to configure your computer a bit. First, you need to tell Postgres where to find the database cluster where your databases will be stored:

```
$ echo "export PGDATA=/usr/local/var/postgres" >> ~/.bash\_profile
```

This command will help some programs find Postgres more easily:

```
$ echo "export PGHOST=/tmp" >> ~/.bash\_profile
```

To load these configuration changes, run:

```
$ source ~/.bash\_profile
```

To start the Postgres server, simply run:

```
$ postgres
```

You’ll have to leave that window open while you need the server. To stop the server, press Ctrl + C (\_not\_ Cmd + C). If you want Postgres to boot at startup and run in the background, run:

```
$ ln -sfv /usr/local/opt/postgresql/\*.plist ~/Library/LaunchAgents
```

And to start it now (since it won’t boot automatically until you restart your computer), run:

```
$ pg\_ctl start
```

create a default database with your computer’s username:

```
$ createdb $USER
```

And you’re done.