DEV Community

Joseph Adediji
Joseph Adediji

Posted on • Originally published at Medium on

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.

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
Enter fullscreen mode Exit fullscreen mode

To install Postgres

$ sudo apt-get install postgresql postgresql-contrib libpq-dev
Enter fullscreen mode Exit fullscreen mode

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 fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ touch .psql\_history
Enter fullscreen mode Exit fullscreen mode

You can now connect to the Postgres server by typing :

$ psql
Enter fullscreen mode Exit fullscreen mode

2. Mac

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

$ brew install postgres
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This command will help some programs find Postgres more easily:

$ echo "export PGHOST=/tmp" >> ~/.bash\_profile
Enter fullscreen mode Exit fullscreen mode

To load these configuration changes, run:

$ source ~/.bash\_profile
Enter fullscreen mode Exit fullscreen mode

To start the Postgres server, simply run:

$ postgres
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ pg\_ctl start
Enter fullscreen mode Exit fullscreen mode

create a default database with your computer’s username:

$ createdb $USER
Enter fullscreen mode Exit fullscreen mode

And you’re done.

Top comments (2)

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

Homebrew makes installing stuff so easy 😅

Collapse
 
midigo0411 profile image
Elvis Otieno

great notes for parrot thanks