Skip to content

9. Shared PostgreSQL server with students

In this exercise, a database server shared with the students is prepared for a PostgreSQL exercise in IsardVDI, without the need for advanced network configurations.

Scenario

                                    ┌────────────────────────┐                    

                                    │                        │                    

                                    │ DEFAULT GATEWAY ROUTER │                    

                                    │                        │                    

                                    └────────────┬───────────┘                    

                                                 │.1                              

                         ┌───────────────────────┼────────────────────────┐       

                         │                       │                        │       

                         │   192.168.1.0/24      │                        │       

                ─────┬───┴─────────      ─────┬──┴─────────     ──────┬───┴───────

                     │   default              │                       │           

                     │                        │                       │           

                ┌────┴─────────┐          ┌───┴──────────┐      ┌─────┴────────┐  

                │              │          │              │      │              │  

                │ server       │          │  client1     │      │ client2      │  

                │              │          │  Ubuntu      │      │ Ubuntu       │  

                │ Postgres     │          │  Postgres    │      │ Postgres     │  

                │              │          │              │      │              │  

                │              │          │              │      │              │  

                │              │          │              │      │              │  

                │              │          │              │      │              │  

                └──────┬───────┘          └──────┬───────┘      └──────┬───────┘  

                       │.20                      │DHCP                 │DHCP      

                       │                         │                     │          

                       │                         │                     │          

                       │     192.168.222.0/24    │                     │          

               ────────┴──────┬──────────────────┴─────────────────────┴──────────

                              │         Group network: grupo_bbdd                  

                              │                                                   

                              │.10                                                

                      ┌───────┴───────┐                                           

                      │               │                                           

                      │               │                                           

                      │  server       │                                           

                      │  DHCP         │                                           

                      │  Alpine       │                                           

                      │               │                                           

                      │               │                                           

                      └───────────────┘                                           

Create resources and users in IsardVDI

To create all the following resources you need the administrator role, from the administration panel (see the administration manual).

First, create a new category with the following name:

If there is no student group, create one:

Create a sample user for this group:

Add a group network, which the students must also see. Networks are created in Domains → Resources, Interfaces section (see Resources):

Then, share it with the students: find the network you created, click the button and select the correct category and group:

Create PostgreSQL server

Next, the PostgreSQL server is created: a desktop with the following characteristics:

Inside the desktop, open a terminal and run the following commands:

sudo bash -c 'cat > /etc/udev/rules.d/70-persistent-net.rules << EOF
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{type}=="1", KERNEL=="enp1s0", NAME="inet"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{type}=="1", KERNEL=="enp2s0", NAME="grupo"
EOF'

DHCP Server with Alpine Linux

The DHCP server is hosted on a desktop based on Alpine Linux, with the following configuration:

Inside the server, update the packages and shut it down:

apk update
apk upgrade
poweroff

Next, edit the desktop with the following characteristics:

  1. Remove the ISO.
  2. Boot: Hard Disk.
  3. Keep only the Default network.

Then, convert the desktop into a template to have a base Alpine template, and check that the template is enabled and has the necessary permissions. This can be changed in the templates section with the buttons and :

Connecting to the router via SSH from a graphical desktop to use the clipboard

Next, edit the desktop to add the group network created earlier and start it again; it now depends on the template you have just created. To be able to copy and paste commands, a graphical desktop with the SPICE viewer is used, and from it you connect via SSH to the DHCP server, which will have a fixed IP.

On the DHCP server you can run setup-alpine again and configure the IP of the new interface, or assign the new IP address with the following commands:

ip link set eth1 up
ip a a 192.168.222.10/24 dev eth1

Next, create a Fedora-based client as follows:

Inside the client, assign another IP from the same group network:

sudo systemctl stop NetworkManager.service 
sudo ip a a 192.168.222.20/24 dev enp2s0
ping -c 1 192.168.222.10

Create the Alpine xarxes template

The goal is to have an Alpine template ready to work with networks, with an initial script where the students can enter basic Linux commands to configure interfaces and iptables.

The script commands run when the machine boots.

  1. Create the script and give it permissions on the Alpine server:
touch /usr/local/bin/startup_script.sh
chmod u+x /usr/local/bin/startup_script.sh
  1. Install the Bash shell:
apk add bash
  1. Enable the forwarding bit for the next boot:
# Enable forwarding bit so it can function as a router
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  1. Script content:
#!/bin/bash
ip a s >> /tmp/log_ip.txt
  1. Create the service file:
touch /etc/init.d/router
chmod u+x /etc/init.d/router
  1. Service file content:
#!/sbin/openrc-run
depend() {
    after sshd
}
start() {
    ebegin "router starting"
    /usr/local/bin/startup_script.sh
    eend $?
}
  1. Enable the service:
rc-update add router default
  1. Reboot and check that it is active by looking at:
cat /proc/sys/net/ipv4/ip_forward # Ha de mostrar un 1
  1. Add the network packages:
apk add iptables iproute2 dnsmasq
  1. Shut down the desktop and create the Alpine xarxes v1 template.

Configure the DHCP server

On the DHCP server desktop, remove the Default network so that only the group interface remains.

Run the Alpine interface configuration:

Then, restart the network to apply the new configuration:

rc-service networking restart

Next, configure the DHCP server by creating the configuration file:

cat <<'EOF' > /etc/dnsmasq.conf
interface=eth0
dhcp-range=192.168.222.20,192.168.222.199,255.255.255.0,24h
log-dhcp
dhcp-leasefile=/var/lib/dnsmasq.leases
EOF

Start the service and enable it to run at boot:

rc-service dnsmasq start
rc-update add dnsmasq

Reboot, check with a client that it gets an IP and that the assignment is recorded in the logs:

Finally, shut down the desktop and create the dhcp-server-alpine template:

PostgreSQL Server on Debian

Create a Debian desktop from an ISO (from Media → Create new desktop) with the following configuration:

Install it as a server, without any graphical environment and selecting SSH server:

With the desktop shut down, set Boot: Hard Disk, remove the ISO and create the debian13-server template:

Install PostgreSQL service

  1. Add the group network interface.
  2. Configure the interface. By default, Debian configures the network with the networking service and the /etc/network/interfaces file.
  3. Become root and add these lines to configure the second network card (enp2s0):
auto enp2s0
iface enp2s0 inet static
    address 192.168.222.20
    netmask 255.255.255.0
  1. Restart the service:
systemctl restart networking
  1. Install sudo:
apt install sudo
usermod -aG sudo isard
echo 'isard ALL=(ALL) NOPASSWD:ALL' | tee /etc/sudoers.d/isard
  1. Log in as isard and check that sudo works without asking for the password.
  2. Install the postgres packages:
sudo apt update
sudo apt install -y postgresql postgresql-contrib
  1. Configure the server so that it is accessible from other machines:
# Change listen_addresses to '*' in postgresql.conf
sudo sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/17/main/postgresql.conf
# Add the line to pg_hba.conf 
echo "host    all             all             0.0.0.0/0               md5" | sudo tee -a /etc/postgresql/17/main/pg_hba.conf
  1. Check that it has been configured correctly:
grep listen_addresses /etc/postgresql/17/main/postgresql.conf
sudo tail -n 1 /etc/postgresql/17/main/pg_hba.conf
  1. Restart the service and enable it:
sudo systemctl restart postgresql
sudo systemctl enable postgresql
  1. Enter the PostgreSQL shell:
sudo -u postgres psql
  1. Create the database and a user, and grant it all privileges:
CREATE DATABASE testdb;

CREATE USER testuser WITH ENCRYPTED PASSWORD '<password>';

GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;
  1. Create a superuser user with all privileges in PostgreSQL:
CREATE USER superuser WITH PASSWORD '<password>';

ALTER USER superuser WITH SUPERUSER;

\q

PostgreSQL Clients in Fedora

  1. Create a desktop based on Fedora 40.
  2. Update the packages and restart the system.
sudo dnf update
reboot

Check that the service works:

psql --version

Install pgadmin4:

flatpak install flathub org.pgadmin.pgadmin4

To install the DBeaver client, download the .rpm file from the official website and install it.

Check that the clients work and can connect.

From the client command line, connect to the PostgreSQL server:

PGPASSWORD="<password>" psql -h 192.168.222.20 -U superuser -d testdb

Create a table:

CREATE TABLE test (
    id SERIAL PRIMARY KEY,
    description TEXT
);

If no errors have occurred, the table should be created.

In pgAdmin4, configure the connection and create a row in the test table:

In DBeaver, also configure the connection:

Deploy users

Finally, create the templates of the PostgreSQL server and of the desktop with the clients.