How to install PostgreSQL on Ubuntu 20.04

This article was first published in: https://www.itcoder.tech/posts/how-to-install-postgresql-on-ubuntu-20-04/

PostgreSQL or Postgres is an open source, multi-purpose relational database management system. It has many advanced features that allow you to build fault-tolerant environments or complex applications.

In this guide, we will explain how to install PostgreSQL database server on Ubuntu 20.04 and explore the basic operations of PostgreSQL database management.

1. Prerequisites##

To install the package, log in as root or another user with sudo privileges.

2. Install PostgreSQL on Ubuntu

At the time of writing this article, the latest available version of PostgreSQL in the official Ubuntu software source is 10.4.

Run the following command to install PostgreSQL on Ubuntu:

sudo apt update
sudo apt install postgresql postgresql-contrib

We also install the PostgreSQL contrib package, which can provide some additional features of the PostgreSQL database system.

Once the installation is complete, the PostgreSQL service will start automatically. Use the psql tool to verify the installation by connecting to the PostgreSQL database and printing its version:

sudo -u postgres psql -c "SELECT version();"

Output:

PostgreSQL 12.2(Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc(Ubuntu 9.3.0-8ubuntu1)9.3.0,64-bit

that's it. PostgreSQL has been installed, and you can start using it.

Three, PostgreSQL roles and authentication methods##

PostgreSQL database access permissions are handled through roles. A role represents a database user or a database user group.

PostgreSQL supports multiple authentication methods. The most commonly used methods are as follows:

PostgreSQL client authentication is usually defined in the pg_hba.conf file. By default, for local connections, PostgreSQL is set to authenticate against peers.

In order to log in to the PostgreSQL server as the postgres user, first switch the user, and then use the psql tool to access PostgreSQL.

sudo su - postgres
psql

From here, you can interact with the PostgreSQL instance. Exit the PostgreSQL Shell and enter:

\ q

You can also use the sudo command to access PostgreSQL without switching users:

sudo -u postgres psql

Usually, the postgres user is only used locally.

Fourth, create a PostgreSQL role and database##

Only super users and roles with CREATEROLE permission can create new roles.

In the following example, we create a role named john, a database named johndb, and grant permissions on the database:

  1. Create a new PostgreSQL role:
sudo su - postgres -c "createuser john"
  1. Create a new PostgreSQL database:
sudo su - postgres -c "createdb johndb"

To authorize users to operate the database, connect to the PostgreSQL shell:

sudo -u postgres psql

And run the following query:

grant all privileges on database johndb to john;

Five, enable remote access to PostgreSQL server##

By default, the PostgreSQL server only listens on the local network interface: 127.0.0.1.

To allow remote access to your PostgreSQL server, open the configuration file postgresql.conf and add listen_addresses ='*' in the section CONNECTIONS AND AUTHENTICATION.

sudo nano /etc/postgresql/12/main/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses ='*'     # what IP address(es) to listen on;

Save the file and restart the PostgreSQL service:

sudo service postgresql restart

Use the ss tool to verify the modification:

ss -nlt | grep 5432

The output shows that the PostgreSQL server is listening on all network interfaces (0.0.0.0):

LISTEN  02440.0.0.0:54320.0.0.0:*              
LISTEN  0244[::]:5432[::]:*

The next step is to configure the server to accept remote connections and edit the pg_hba.conf file.

Here are some examples showing different user scenarios:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# The user jane can access all databases from all locations using md5 password
host    all             jane            0.0.0.0/0                md5

# The user jane can access only the janedb from all locations using md5 password
host    janedb          jane            0.0.0.0/0                md5

# The user jane can access all databases from a trusted location(192.168.1.134) without a password
host    all             jane            192.168.1.134            trust

The last step is to open port 5432 on your firewall.

Assuming you are using UFW to manage your firewall, and you want to allow access from the 192.168.1.0/24 subnet, you should run the following command:

sudo ufw allow proto tcp from192.168.1.0/24 to any port 5432

Make sure that your firewall is configured and only accept connections from trusted IP ranges.

Six, summary##

We have shown you how to install and configure PostgreSQL on an Ubuntu 20.04 server. Browse PostgreSQL 12 Documentation for more information on this topic.

If you have any questions, please contact us in the following ways:

WeChat: sn0wdr1am86

WeChat group: add the above WeChat, remark the WeChat group

QQ: 3217680847

QQ Group: 82695646

Recommended Posts

How to install PostgreSQL on Ubuntu 16.04
How to install PostgreSQL on Ubuntu 20.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install Postgresql on CentOS 8
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to install MySQL on Ubuntu 18.04 (linux)
How to install Ubuntu 19.10 on Raspberry Pi 4
How to install Apache Kafka on Ubuntu 18.04
How to set PostgreSQL startup on Ubuntu 16.04
How to install Apache Tomcat 8 on Ubuntu 16.04