Charmed PostgreSQL K8s Tutorial > 2. Deploy PostgreSQL
Deploy Charmed PostgreSQL K8s
In this section, you will deploy Charmed PostgreSQL K8s, access the units hosting this charm, and interact with the PostgreSQL databases that exist inside the application.
Note: All commands are written for juju >= v.3.0
If you are using an earlier version, be aware that:
juju run
replacesjuju run-action --wait
injuju v.2.9
juju integrate
replacesjuju relate
andjuju add-relation
injuju v.2.9
For more information, check the Juju 3.0 Release Notes.
Deploy
To deploy Charmed PostgreSQL, all you need to do is run
juju deploy postgresql-k8s --channel 14/stable --trust
--trust
is required because the charm and Patroni need to create some K8s resources.
Juju will now fetch Charmed PostgreSQL K8s from Charmhub and deploy it to the local MicroK8s. This process can take several minutes depending on how provisioned (RAM, CPU, etc) your machine is.
You can track the progress by running:
juju status --watch 1s
This command is useful for checking the real-time information about the state of a charm and the machines hosting it. Check the juju status
documentation for more information about its usage.
When the application is ready, juju status
will show something similar to the sample output below:
Model Controller Cloud/Region Version SLA Timestamp
tutorial charm-dev microk8s/localhost 2.9.42 unsupported 12:00:43+01:00
App Version Status Scale Charm Channel Rev Address Exposed Message
postgresql-k8s active 1 postgresql-k8s 14/stable 56 10.152.183.167 no
Unit Workload Agent Address Ports Message
postgresql-k8s/0* active idle 10.1.188.206
You can also watch juju logs with the juju debug-log
command.
More info on logging in the juju logs documentation.
Access PostgreSQL
Warning: This part of the tutorial accesses PostgreSQL via the operator
user.
Do not directly interface with the operator
user in a production environment.
In a later section about Integrations, we will cover how to safely access PostgreSQL by creating a separate user via the Data Integrator charm
Connecting to the database requires that you know the values for host
, username
and password
.
To retrieve these values, run the Charmed PostgreSQL K8s action get-password
:
juju run postgresql-k8s/leader get-password
Running the command above should output:
unit-postgresql-k8s-0:
UnitId: postgresql-k8s/0
id: "2"
results:
password: SYhCduijXTAfg9mU
status: completed
timing:
completed: 2023-03-20 11:01:26 +0000 UTC
enqueued: 2023-03-20 11:01:24 +0000 UTC
started: 2023-03-20 11:01:25 +0000 UTC
To request a password for a different user, use the option username
:
juju run postgresql-k8s/leader get-password username=replication
The IP address of the unit hosting the PostgreSQL application, also referred to as the “host”, can be found with juju status
:
...
Unit Workload Agent Address Ports Message
postgresql-k8s/0* active idle 10.1.188.206
...
To access the units hosting Charmed PostgreSQL K8s run
juju ssh --container postgresql postgresql-k8s/leader bash
If at any point you’d like to leave the unit hosting Charmed PostgreSQL K8s, enter Ctrl+D
or type exit
.
The easiest way to access PostgreSQL is via the PostgreSQL interactive terminal psql
, which is already installed here.
To list all available databases, run:
psql --host=10.1.188.206 --username=operator --password --list
When requested, enter the <password>
for charm user operator
that you obtained earlier.
Example output:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | operator | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | operator | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/operator +
| | | | | operator=CTc/operator
template1 | operator | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/operator +
| | | | | operator=CTc/operator
(3 rows)
You can now interact with PostgreSQL directly using PostgreSQL SQL Queries. For example, entering SELECT version();
should output something like:
> root@postgresql-k8s-0:/# psql --host=10.1.188.206 --username=operator --password postgres
Password:
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
Type "help" for help.
postgres=# SELECT version();
version
---------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, 64-bit
(1 row)
Feel free to test out any other PostgreSQL queries.
When you’re ready to leave the PostgreSQL shell, you can just type exit
. This will take you back to the host of Charmed PostgreSQL K8s (postgresql-k8s/0
). Exit this host by once again typing exit
. Now you will be in your original shell where you first started the tutorial. Here you can interact with Juju and MicroK8s.