How to Install PostgreSQL on Ubuntu, Linux & Mac

How to Install PostgreSQL on Ubuntu, Linux & Mac

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.

Image for post

Image for post

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 -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.