PostgreSQL

Attention!

Zone does not provide official support for PostgreSQL! PostgreSQL is an unmanaged service.
This guide is intended for advanced users who are familiar with PostgreSQL administration and have a good knowledge of the Linux shell. The guide only covers how to start the PostgreSQL service on the Zone platform (PoC). Further administration, user and database management, backups, etc. are all the responsibility of the user!

1. Log in to a virtual server using SSH

Establishing an SSH connection.

2. Create a PostgreSQL database

initdb -D $HOME/postgresql
echo "listen_addresses = '$(vs-loopback-ip -4)'" >> $HOME/postgresql/postgresql.conf
echo "unix_socket_directories = '$HOME/tmp'" >> $HOME/postgresql/postgresql.conf

3. Create PM2 configuration file

The PM2 file is needed to start the PostgreSQL service automatically. In the filename, the .config.js ending is important!

Open the file ~/postgresql/postgresql.config.js and add:

module.exports = {
  apps: [
    {
      name: "postgresql",
      script: "/opt/zse/bin/postgres",
      args: "-D " + process.env.HOME + "/postgresql/",
      exec_mode: "fork", // Ensures it runs in the background
      autorestart: true, // Restart if it crashes
      watch: false, // PM2 will not watch for file changes
      max_restarts: 10, // Maximum number of restarts before giving up
    },
  ],
};

4. Add PM2 app to My Zone control panel

In the web-hosting management of the My Zone control panel, select PM2 and Node.js and then Add New Application.
Enter Postgresql in the Application Name field.
In the script or PM2 .JSON field enter: postgresql/postgresql.config.js.
Maximum memory usage: drag the slider to the maximum.
Click Save.

Wait 3-4 minutes until pm2 list in shell displays postgresql application status: online.

5. Set the root user password!

Very important!

By default, the root user is without a password and an external user can access your PostgreSQL server!
Choose a long (at least 14 characters) and secure password and store it safely!

The root user name is your SSH user name in the form virtXXX, which is what you see when you run the shell: whoami

The PostgreSQL server host is your loopback IP address, which you can see if you run the shell: vs-loopback-ip -4

To set a password, log in to the PostgreSQL server:

psql -h $(vs-loopback-ip -4) -U $(whoami) -d postgres

Replace new_password with a secure password and run:

ALTER USER SESSION_USER WITH PASSWORD 'new_password';

Then remove the “trust” method from the pg_hba.conf file (without it the password will not be prompted):

sed -i 's/trust$/md5/g' ~/postgresql/pg_hba.conf

Restart the postgresql server:

pm2 restart postgresql

Make sure you are now prompted for a password when you connect!

psql -h $(vs-loopback-ip -4) -U $(whoami) -d postgres

6. Notes

  • NB! There is only one version of PostgreSQL available on the ZoneOS server (currently 14.5) and this may change with a ZoneOS update without notice!
  • It is a good idea to regularly back up the database yourself, as the Zone default file system backup may not be perfect.
  • If you want to use PostgreSQL with PHP, you need to activate the PostgreSQL extension under PHP extensions in the My Zone control panel.
Updated on 12. Nov 2024
Was this article helpful?

Related Articles