avatarJoseph Adediji

Summary

The website provides a step-by-step guide for installing PostgreSQL on Ubuntu, Linux, and Mac systems.

Abstract

The article is a comprehensive tutorial aimed at assisting individuals in setting up PostgreSQL on their systems. It details the necessary commands for installation on Ubuntu, including updating the package list, installing PostgreSQL and its associated packages, and creating a superuser role with a personalized database. For those using elementary or parrot operating systems, there are additional instructions to start the PostgreSQL service and create a user with superuser privileges. The guide also covers the creation of a .psql_history file to save command history. For Mac users, the article explains how to use Homebrew for installation, configure the system to locate the database cluster, and ensure that Postgres can be found by applications. It also provides commands for starting and stopping the PostgreSQL server and setting it up to run at startup. The author emphasizes the importance of following the steps

How to Install PostgreSQL on Ubuntu, Linux & Mac

So, recently I had to reinstalled 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.

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.

If you liked this article please share it with your network and don’t forget to follow me for more great articles and tutorials.

Postgresql
Postgres
Ubuntu
Mac
Linux
Recommended from ReadMedium