PostgreSQL in Ubuntu...under construction ;)

By Salerno | October 20, 2022

# 1) Super user
sudo su

# 2) Looking for postgresql versions

apt-cache search postgresql-9.*

# 3) Installing on Ubuntu

# 3.1) Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# 3.2) Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# 3.3) Update the package lists:
sudo apt-get update

# 3.4) Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql

# 3.5) Checking versions avalilable
apt-cache search postgresql

# 4) Knowing information about an package as an example
apt show postgresql-9.6-wal2json

# 5) Becoming an postgresql user
su postgres

# 6) PostgreSQL

# 6.1) Entering PostgreSQL
psql

# 6.2) Checking version
select version();

# 6.3) To leave...
\q

# 7) Checking clusters (example)
pg_lsclusters

# 7.1) Output
#Ver Cluster Port Status Owner    Data directory                 Log file
#14  main    5432 online postgres /var/lib/postgresql/14/main  
#/var/log/postgresql/postgresql-14-main.log
#15  main    5433 online postgres /var/lib/postgresql/15/main #/var/log/postgresql/postgresql-15-main.log

# 8) Creating a cluster (a bunch of databases inside a repo)
# If you are looking for the config files, check it out here: computer/etc/postgresql/<VERSION>/main
# To find out where the databases are, you need go to: computer/var/lib/postgresql/<VERSION>
# To manage the DB, the files are: computer/lib/postgresql/15/bin
pg_createcluster 15 teste

# 8.1) Starting....
pg_ctlcluster 15 teste start #or
sudo systemctl start postgresql@15-teste

# 8.2) Stopping...
pg_ctlcluster 15 teste stop

# 8.3) Reloading....
pg_ctlcluster 15 teste reload

# 8.4) Drop
pg_dropcluster 15 teste

# 9) Listing databases
psql
\l

# 10) Accessing template1
# template0 is imutable
\c template1

DB List. Source: DFS


# 11) Create table
create table usuarios(id serial primary key, nome varchar(100));

create database dbnovo;
\c dbnovo
# list of relations
\dt

# 12) Using initdb to activate our DB

# 12.1) Accesing 
cd usr/lib/postegresql/15/bin

# 12.2) Creating a new dir
mkdir /var/lib/postgresql/15/teste

# 12.3) Environment preparing
# We've created the environment but it is not online
./initdb -D /var/lib/postgresql/15/teste

# 12.4) Online...
./pg_ctl -D /var/lib/postgresql/15/teste -l /tmp/postgresql.log start

# 12.5) Stopped...
./pg_ctl -D /var/lib/postgresql/15/teste -l /tmp/postgresql.log stop

# 13) More than one cluster...

# 13.1) Accessing (only if the cluster is online)

psql -p 5433
comments powered by Disqus