See this guide for more information.
IMPORTANT: xxx below are placeholders. Update with your app name, user name etc.
-
Add the PostgreSQL APT repository:
sudo apt-get update sudo apt-get install -y postgresql-common sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh sudo apt-get update
-
Install postgresql:
sudo apt-get install postgresql-18
-
Open
/etc/postgresql/*/main/postgresql.confand change the following:sudo vim /etc/postgresql/18/main/postgresql.conf
listen_addresses = '*' # ... shared_preload_libraries = 'pg_stat_statements' # .... # # INSERT Settings from https://pgtune.leopard.in.ua/#/ below # Make sure you select `Online transaction processing system` in DB Type #
-
Go to PgTune, select
Online transaction processing systemin DB Type and generate the configuration for the database. Add the configuration at the end of/etc/postgresql/*/main/postgresql.confsudo vim /etc/postgresql/18/main/postgresql.conf
-
Change the postgres user password:
sudo -u postgres psql
ALTER USER postgres WITH PASSWORD '<password>';
-
Create the new users/databases as required:
CREATE USER xxx WITH password 'pass'; GRANT xxx to postgres; CREATE DATABASE xxx_prod OWNER xxx;
-
Log in as the newly created user to the create db and create
pg_stat_statementsextension:CREATE EXTENSION pg_stat_statements;
-
Allow connections from any host. Append the following at the end of
/etc/postgresql/*/main/pg_hba.conf:sudo vim /etc/postgresql/18/main/pg_hba.conf
# Private network host all all 10.0.0.0/16 scram-sha-256# Internet: ONLY if required. We normally don't need this as we connect externally using an # SSH tunnel. # hostssl all all 0.0.0.0/0 scram-sha-256 # hostssl all all ::/0 scram-sha-256
-
Update the postgresql service to restart on failure:
sudo systemctl edit postgresql@18-main
And add the following lines in the file that opens up:
[Service] Restart=always RestartSec=5
-
Restart the postgresql service:
sudo systemctl daemon-reload sudo systemctl restart postgresql.service
-
Check that the service starts properly:
sudo less /var/log/postgresql/postgresql-18-main.log
-
Test by connecting to the database from the local machine.
-
Install the aws-cli:
sudo snap install aws-cli --classic
-
Configure the aws-cli with the credentials of the
db-syncersuser under therbtzprofile:aws configure --profile rbtz
-
Install
jqandzstd:sudo apt-get install jq zstd
-
Add the following at the end of the
~/.bashrcfile:if [ -f ~/.env ]; then source ~/.env fi
-
Add the relevant DB passwords to the
~/.envfile:export XXX_PROD_DB_PASS="<password>"
-
Place the scripts in a directory like
/home/xxx/data-syncers.Please update the scripts with the correct paths after you have created the directory.
-
Copy the
cron-filein the repository to/etc/cron.d/xxx-db-backups.sudo cp /home/xxx/data-syncers/cron-file /etc/cron.d/xxx-db-backups
-
Check that the cron job was loaded successfully using:
sudo service cron status