Tutorial

How To Install PostgreSQL on Ubuntu 20.04 [Quickstart]

Updated on March 18, 2022
Default avatar

By Mark Drake

Manager, Developer Education

English
How To Install PostgreSQL on Ubuntu 20.04 [Quickstart]
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

PostgreSQL, or Postgres, is a relational database management system that provides an implementation of the SQL querying language. It’s standards-compliant and has many advanced features like reliable transactions and concurrency without read locks.

This guide demonstrates how to quickly get Postgres up and running on an Ubuntu 20.04 server, from installing PostgreSQL to setting up a new user and database. If you’d prefer a more in-depth tutorial on installing and managing a PostgreSQL database, see How To Install and Use PostgreSQL on Ubuntu 20.04.

Simplify creating PostgreSQL databases with DigitalOcean Managed Database. Create a Postgres database in minutes and let DigitalOcean handle migrating your data, upgrades, maintenance, and security.

Prerequisites

To follow along with this tutorial, you will need one Ubuntu 20.04 server that has been configured by following our Initial Server Setup for Ubuntu 20.04 guide. After completing this prerequisite tutorial, your server should have a non-root user with sudo permissions and a basic firewall.

Step 1 — Installing PostgreSQL

To install PostgreSQL, first refresh your server’s local package index:

  1. sudo apt update

Then, install the Postgres package along with a -contrib package that adds some additional utilities and functionality:

  1. sudo apt install postgresql postgresql-contrib

Ensure that the service is started:

  1. sudo systemctl start postgresql.service

Step 2 — Using PostgreSQL Roles and Databases

By default, Postgres uses a concept called “roles” to handle authentication and authorization. These are, in some ways, similar to regular Unix-style users and groups.

Upon installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

The installation procedure created a user account called postgres that is associated with the default Postgres role. There are a few ways to utilize this account to access Postgres. One way is to switch over to the postgres account on your server by running the following command:

  1. sudo -i -u postgres

Then you can access the Postgres prompt by running:

  1. psql

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

To exit out of the PostgreSQL prompt, run the following:

  1. \q

This will bring you back to the postgres Linux command prompt. To return to your regular system user, run the exit command:

  1. exit

Another way to connect to the Postgres prompt is to run the psql command as the postgres account directly with sudo:

  1. sudo -u postgres psql

This will log you directly into Postgres without the intermediary bash shell in between.

Again, you can exit the interactive Postgres session by running the following:

  1. \q

Step 3 — Creating a New Role

If you are logged in as the postgres account, you can create a new role by running the following command:

  1. createuser --interactive

If, instead, you prefer to use sudo for each command without switching from your normal account, run:

  1. sudo -u postgres createuser --interactive

Either way, the script will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user to your specifications.

Output
Enter name of role to add: sammy Shall the new role be a superuser? (y/n) y

Step 4 — Creating a New Database

Another assumption that the Postgres authentication system makes by default is that for any role used to log in, that role will have a database with the same name which it can access.

This means that if the user you created in the last section is called sammy, that role will attempt to connect to a database which is also called “sammy” by default. You can create the appropriate database with the createdb command.

If you are logged in as the postgres account, you would type something like the following:

  1. createdb sammy

If, instead, you prefer to use sudo for each command without switching from your normal account, you would run:

  1. sudo -u postgres createdb sammy

Step 5 — Opening a Postgres Prompt with the New Role

To log in with ident based authentication, you’ll need a Linux user with the same name as your Postgres role and database.

If you don’t have a matching Linux user available, you can create one with the adduser command. You will have to do this from your non-root account with sudo privileges (meaning, not logged in as the postgres user):

  1. sudo adduser sammy

Once this new account is available, you can either switch over and connect to the database by running the following:

  1. sudo -i -u sammy
  2. psql

Or, you can do this inline:

  1. sudo -u sammy psql

This command will log you in automatically, assuming that all of the components have been properly configured.

If you want your user to connect to a different database, you can do so by specifying the database like the following:

  1. psql -d postgres

Once logged in, you can get check your current connection information by running:

  1. \conninfo
Output
You are connected to database "sammy" as user "sammy" via socket in "/var/run/postgresql" at port "5432".

Conclusion

You are now set up with PostgreSQL on your Ubuntu 20.04 server. If you’d like to learn more about Postgres and how to use it, we encourage you to check out the following guides:

Want to launch a high-availability PostgreSQL cluster in a few clicks? DigitalOcean offers worry-free PostgreSQL managed database hosting. We’ll handle maintenance and updates and even help you migrate your database from external servers, cloud providers, or self-hosted solutions. Leave the complexity to us, so you can focus on building a great application.

Learn more here


About the authors
Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
6 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These tips are great! Thanks alot, man!

Being a lifelong MySQL/MariaDB user, I had no idea where to start when I found myself needing a local Postgres install. As usual, after a simple Google search, a DO tutorial was one of the first results. Also, as usual, the tutorial was great and saved me a lot of time looking elsewhere.

Not sure about the complaints above, but there is nothing missing from this simple guide. If this great and concise write up is too challenging, then you should probably look into the graphical installer for Windows because you are clearly not of sufficient skill level to follow 5 simple steps.

Thanks for the great tutorial.

Nice tutorial. Helped me get setup in under 5 minutes. Thanks for making my life easier.

this short guide is detailed and useful, thanks!

Nice guide! Just be aware that will will load the apt default repo version (12). As of this writing it’s two versions behind the Postgres current (14).

This should really be a ‘Quickstart for people who can remember all the steps in between these steps’ guide. I’m having a difficult time following the instructions in this tutorial while setting up a new server. You seem to be jumping key steps without regard for users trying to follow instructions systematically. To make it worse, I’m feeling extra stupid as I’ve I’ve been using PG for a couple years. But, one sets up a server only once in awhile so having some order to get things off on the right track is key imo for quickstart-type guides. Thanks!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel