Transferring a database to a dedicated server

If you want to host the database separately from the iBatyr Monitor service and the web interface, prepare the database server and connect the iBatyr Monitor server to it.

Work on the database server

Install the software

  1. Install a clean Ubuntu Server version 20 or 22.

  2. Update packages on the system:

sudo apt update && sudo apt upgrade
  1. Install packages to install the database:

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https lsb-release curl -y
  1. Add keys to the PostgreSQL repository:

curl -fSsL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql.gpg > /dev/null
  1. Add a repository:

echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main | sudo tee /etc/apt/sources.list.d/postgresql.list
  1. Update the repository lists in the package manager:

sudo apt update
  1. Install PostgreSQL 15:

sudo apt install postgresql-client-15 postgresql-15 -y

Create a user

  1. Find the password on the iBatyr Monitor server using the command:

cat /etc/ibatyr/config | grep PASSWORD
  1. Copy the password. It will be in quotes, for example:

'PASSWORD': '05ce255df14c3308143737316139b77',
  1. Insert the password into the command instead of пароль and execute it on the database server:

sudo -u postgres psql -c "create user ibatyr with password 'пароль';"

Example:

sudo -u postgres psql -c "create user ibatyr with password '05ce255df14c3308143737316139b77';"

Change the configuration

  1. Open the configuration file /etc/postgresql/15/main/postgresql.conf for editing.

The example uses Vim, but any editor will do:

sudo vim /etc/postgresql/15/main/postgresql.conf

Find the line:

#listen_addresses = 'localhost,00.00.00.000'             # what IP address(es) to listen on;

Remove the # symbol at the beginning of the line and add the IP through which iBatyr Monitor will access the database server.

Note

It is important to specify the exact IP address of the database server. This address defines the interface through which the server accepts requests. It is not associated with the IP address from which requests are coming.

Save your changes and exit the editor.

  1. Open the file /etc/postgresql/15/main/pg_hba.conf for editing:

sudo vim /etc/postgresql/15/main/pg_hba.conf

At the end of the file add:

host    all             all             {ip сервера iBatyr Monitor}/{маска}        md5

For example, if 00.00.00.000 is the IP address of the iBatyr Monitor server and 32 is the subnet mask:

host    all             all            00.00.00.000/32         md5
  1. Restart PostgreSQL to apply the changes:

sudo systemctl restart postgresql

Work on the iBatyr Monitor server

Migrate the database

Note

If there is no data on the server, skip this step and go to the Create a database section.

  1. Turn off the iBatyr Monitor server:

ibatyr stop
  1. Create a backup with the command:

ibatyr backup_db
  1. Transfer the backup copy from the /var/lib/ibatyr/ibatyr_backup/ibatyr-db.dump directory to the database server.

  2. Start database recovery on the remote server:

sudo -u postgres pg_restore --verbose --clean --create --format=c {путь до файла бекапа} -d postgres

For example:

sudo -u postgres pg_restore --verbose --clean --create --format=c /home/ibatyr-db.dump -d postgres

Create a database

  1. On the database server, connect to PostgreSQL:

sudo -u postgres psql
  1. Create a database:

CREATE DATABASE ibatyr WITH OWNER = ibatyr ENCODING = 'UTF8' LC_COLLATE = 'ru_RU.UTF-8' LC_CTYPE = 'ru_RU.UTF-8' TEMPLATE = template0;
  1. Select the created database and create the necessary extensions:

\c ibatyr
CREATE EXTENSION pg_trgm;
CREATE EXTENSION ltree;
CREATE EXTENSION cube;
CREATE EXTENSION dblink;
CREATE EXTENSION pgcrypto;
  1. Copy all files from the /usr/share/ibatyr/data/ispell/ directory on the iBatyr Monitor server to the /usr/share/postgresql/15/tsearch_data/ directory.

For example, using the scp command:

scp -r /usr/share/ibatyr/data/ispell/* user@10.10.10.2:/usr/share/postgresql/15/tsearch_data/

where user@10.10.10.1 is the user name and ip the server address iBatyr Monitor, and user@10.10.10.2 is the user name and the address of the server with the database.

  1. Initialize the database on the iBatyr Monitor server:

ibatyr init
  1. Ready! The database has been moved to a dedicated server.

Connect the database

  1. Turn off the iBatyr Monitor server:

ibatyr stop
  1. Open the iBatyr Monitor configuration file /etc/ibatyr/config:

sudo vim /etc/ibatyr/config

Find the block:

DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'ibatyr',
       'USER': 'ibatyr',
       'PASSWORD': '05ce255df14c3308143737316139b77',
       'HOST': '',
       'PORT': '',
   }
}

Specify the address and port of the remote PostgreSQL server in HOST and PORT, for example:

DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'ibatyr',
       'USER': 'ibatyr',
       'PASSWORD': '05ce255df14c3308143737316139b77',
       'HOST': '10.10.13.243',
       'PORT': '5432',
   }
}
  1. Disable automatic database startup on the iBatyr Monitor server:

sudo systemctl stop postgresql
sudo systemctl disable postgresql
  1. Check your database connection:

ibatyr sql

If there are no errors and you see the PostgreSQL prompt (ibatyr=>), the connection was successful.

Exit the prompt:

\q
  1. Initialize the database:

ibatyr init
  1. Ready! The database has been moved to a dedicated server.