PostgreSQL
PostgreSQL is a powerful, open source object-relational database system that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads.
Difficulty: Medium
Installation
Install PostgreSQL using the apt package manager
With user
admin
, update and upgrade your OS
Import the repository signing key
Expected output:
Create the repository configuration file
Update the package lists and install the latest version of PostgreSQL. Press "y" and
enter
or directlyenter
when the prompt asks you
Check the correct installation of the PostgreSQL
Example of expected output:
Ensure PostgreSQL is running and listening on the default port
5432
Expected output:
You can monitor general logs by the systemd journal. You can exit monitoring at any time with
Ctrl-C
Example of expected output:
And the sub-instance and specific cluster logs. You can exit monitoring at any time with
Ctrl-C
Example of expected output:
Create data folder
Create the dedicated PostgreSQL data folder
Assign as the owner to the
postgres
user
Assign permissions of the data folder only to the
postgres
user
With user
postgres
, create a new cluster on the dedicated folder
Edit the PostgreSQL data directory on configuration, to redirect the store to the new location
Replace the
line 42
with/var/lib/postgresql/17/main
to the next. Save and exit
Restart PostgreSQL to apply changes and monitor the correct status of the main instance and sub-instance monitoring sessions before
You can monitor the PostgreSQL main instance by the systemd journal and check the log output. You can exit the monitoring at any time with
Ctrl-C
Expected output:
You can monitor the PostgreSQL sub-instance by the systemd journal and check log output. You can exit monitoring at any time with
Ctrl-C
Example of the expected output:
You can check if the cluster is on status "online" by
Expected output:
(Optional) -> If you want, you can disable the autoboot option for PostgreSQL (not recommended) using:
Expected output:
Ensure PostgreSQL is listening on the default relational database port
Expected output:
Create a PostgreSQL user account
Create a new database
admin
user and assign the password "admin
" with the automatically created user for the PostgreSQL installation, calledpostgres
Expected output:
Congrats! You have PostgreSQL ready to use as a database backend by another software
Extras (optional)
Some useful PostgreSQL commands
With user
admin
, enter the PostgreSQL CLI with the userpostgres
. The prompt should change topostgres=#
Example of expected output:
Type \q
command and enter to exit PostgreSQL CLI and exit to come back to the admin
user
List the global existing users and roles associated
Type the next command and enter
Example of expected output:
List the global existing databases
Type the next command and enter
Example of expected output:
List tables inside of a specific database
Connect to a specific database, type the next command, and enter. The prompt should change to the name of the database. Example:
lndb=#
Replace <NAMEOFDATABASE
> to the specific name of the database
Example:
Expected output:
List tables
Example of expected output:
View the size of a specific database
Type the next command and enter
Replace <NAMEOFDATABASE
> to the specific name of the database
Example:
Example of expected output:
View the size of a specific table inside a database
Enter a specific database with
Replace <NAMEOFDATABASE>
to the specific name of the database
Example:
View the size of a specific table
Replace <NAMEOFTABLE>
to the specific name of the database
Example:
Example of expected output:
Detele a specific database
Type the next command and enter
Replace <NAMEOFTABLE>
to the specific name of the table
Example:
Expected output:
Delete a table inside of a specific database
Stop the service related to this database before the action, i.e: sudo systemctl stop lnd
Enter a specific database with
Replace <NAMEOFDATABASE>
to the specific name of the database
Example:
Delete a specific table
Stop the service related to this table and database before the action, i.e: sudo systemctl stop lnd
Replace <NAMEOFTABLE>
to the specific name of the table
Example:
Upgrade
To upgrade, type this command
Uninstall
Uninstall PostgreSQL package and configuration
With user
admin
, stop and disable the postgres service
Uninstall PostgreSQL using the apt package manager
Uninstall possible unnecessary dependencies
Delete configuration files and data
Uninstall postgres user
Delete the postgres user. Don't worry about
userdel: bitcoind mail spool (/var/mail/bitcoind) not found
output, the uninstall has been successful
Delete postgres group
Delete the complete
postgresdb
directory
Port reference
Last updated