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 22.04 server, from installing PostgreSQL to setting up a new user and database.
Outline
Prerequisites
To follow along with this tutorial, you will need one Ubuntu 22.04 server that has been configured by following our Initial Server Setup for Ubuntu 22.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:
$ sudo apt update
Then, install the Postgres package along with a -contrib package that adds some additional utilities and functionality:
$ sudo apt install postgresql postgresql-contrib
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 typing:
$ sudo -i -u postgres
Then you can access the Postgres prompt by typing:
$ 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:
postgres=# \q
This will bring you back to the postgres Linux command prompt. To return to your regular system user, run the exit command:
postgres=# exit
Another way to connect to the Postgres prompt is to run the psql command as the postgres account directly with sudo:
$ 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 typing:
postgres=# \q
Step 3 — Creating a New Role
If you are logged in as the postgres account, you can create a new role by typing:
$ createuser --interactive
If, instead, you prefer to use sudo for each command without switching from your normal account, type:
$ 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.
OutputEnter name of role to add: sammyShall 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:
postgres=# createdb sammy
If, instead, you prefer to use sudo for each command without switching from your normal account, you would type:
$ 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