How to install Postgresql 13 on Ubuntu 20.04

The following commands will help you quickly install Postgresql latest version 13 or a custom version on your Ubuntu/Debian machine

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql

Of course you can also install some other packages that would help during software development or usage:

postgresql-client-12 : client libraries and client binaries
postgresql-contrib-9.x : additional supplied modules (part of the postgresql-xx package in version 10 and later)
libpq-dev : libraries and headers for C language frontend development
postgresql-server-dev-12 : libraries and headers for C language backend development
pgadmin4 : pgAdmin 4 graphical administration utility

Add to my src(0)

No account yet? Register

Install PostgreSQL on Fedora with password login

sudo dnf update
sudo dnf install postgresql postgresql-server
sudo postgresql-setup initdb 
sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo -u postgres psql

To change default postgres user password, create a database and grant all privileges to that user:

postgres=# \password postgres
Enter new password: 
Enter it again: 
postgres=# create database mydb;
CREATE DATABASE
postgres=# grant all privileges on database mydb to postgres;
GRANT

postgres=# SHOW config_file;
               config_file               
-----------------------------------------
 /etc/postgresql/12/main/postgresql.conf
(1 row)

postgres=# SHOW hba_file;
              hba_file               
-------------------------------------
 /etc/postgresql/12/main/pg_hba.conf
(1 row)

postgres-# \q

To make this change effective, changes in pg_hba.conf are necessary:

sudo vim /var/lib/pgsql/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident


service postgresql restart
sudo -u postgres psql
Password for user postgres:
postgres=# \q
Add to my src(0)

No account yet? Register