SmartVPS - the complete multi-account hosting solution! Each individual account comes with free backups, addon domains, PHP-FPM with OPcache and server-side caching for lightning-fast sites. And all this at a great price!

In this tutorial, we will show you how to configure a PostgreSQL database server on your account using the WebApps platform.

If you do not see a WebApps section in the Control Panel of your hosting account, this tutorial may not be suitable for your particular hosting environment. Please contact our support team for more information.

You need SSH access to the server in order to make the changes described in this tutorial. If you haven't set up SSH for your account yet, you should do this now.

Quick Setup

If you want to quickly set up a PostgreSQL server running on your account, you can download this PostgreSQL setup shell script to your account and run it. The script will set up the WebApps project and the PostgreSQL instance on your account and you will be able to start using them immediately.

In order to run the setup script, you have to change its permissions to 755 after you download it to your account. You will then be able to run it from the command line:

chmod 755
./ postgres1

In that case, you can skip the "PostgreSQL Server Setup" and "Custom WebApps Project" sections of this article and go directly to the Creating Databases and Roles section.

PostgreSQL Server Setup

The PostgreSQL binaries are already installed on the server. You only have to set up a PostgreSQL instance to handle your databases using the WebApps platform.

To do that, you first have to create the directory structure to hold your data on the server. You can use the private/ directory that is available in each hosting account for this:


mkdir -m 0700 "$pg_app_dir"
mkdir -m 0700 "$pg_app_dir/run"
mkdir -m 0700 "$pg_app_dir/sureapp"

touch "$pg_app_dir/.psql_history"
chmod 0600 "$pg_app_dir/.psql_history"

The next step is to initialize your database cluster and set up a superuser role. By default, the username of the superuser role is the same as the username of your hosting account ("example" in this case):

superuser_pass="$(pwgen 24 1)"
touch "$pg_app_dir/.superuser-pwfile"
chmod 0600 "$pg_app_dir/.superuser-pwfile"
echo "$superuser_pass" > "$pg_app_dir/.superuser-pwfile"

for pg_install_dir in $(find /usr/lib/postgresql -maxdepth 1 -type d -o -type l 2>/dev/null | sort -Vr)
    [ -x "$pg_install_dir/bin/initdb" ] && break
"$pg_install_dir/bin/initdb" -D "$pg_app_dir/pgdata" --auth=md5 --pwfile="$pg_app_dir/.superuser-pwfile" && rm -f "$pg_app_dir/.superuser-pwfile"

touch "$pg_app_dir/.pgpass"
chmod 0600 "$pg_app_dir/.pgpass"
printf "*:*:*:%s:%s\n" "$USER" "$superuser_pass" > "$pg_app_dir/.pgpass"

Now, you have to configure the PostgreSQL server to listen for requests using a UNIX socket at the correct location in your account:

sed -i "s~^#*unix_socket_directories = .*\(\s*#.*\)~unix_socket_directories = '$pg_app_dir/run' \1~g" "$pg_app_dir/pgdata/postgresql.conf"
sed -i "s~^#*unix_socket_permissions = .*\(\s*#.*\)~unix_socket_permissions = 0700 \1~g" "$pg_app_dir/pgdata/postgresql.conf"
sed -i "s~^#*listen_addresses = .*\(\s*#.*\)~listen_addresses = '' \1~g" "$pg_app_dir/pgdata/postgresql.conf"

To make starting the PostgreSQL service easier, the following commands will create a startup script:

printf '#!/bin/sh\nexec %s/bin/postgres -D %s/pgdata\n' "$pg_install_dir" "$pg_app_dir" > "$pg_app_dir/"
chmod 0700 "$pg_app_dir/"

In order to use various PostgreSQL management tools such as psql easily, you should also make some changes to your .bashrc file:

grep -q '^PGHOST=' "/home/$USER/.bashrc" || cat <<BASHRC >> "/home/$USER/.bashrc"

# $sureapp_project client configuration
export PGHOST
. "/home/$USER/.bashrc"

Custom WebApps Project

To run the database server, you have to set up a PostgreSQL service. There are two ways to do this:

  • You can create the project on the WebApps page of the Control Panel of your hosting account;
  • You can use the command line interface of the WebApps platform to create the project.

The following sections describe how to do this.

On the WebApps Page of the Control Panel

If you choose the Control Panel way, you should create a new app with the following settings:

  • Engine: Custom
  • Name: postgres1 - The name of your app.
  • Domain: - You can choose any domain from the list. It will not affect the operation of the PostgreSQL server.
  • Subdomain: www - Again, you can choose any subdomain. It will have no effect because the PostgreSQL instance will not be listening on a TCP port.
  • Web access path: /Efu6dunox0vieyelahch2eaf - The web access path should simply be a dummy path that is unlikely to be used by your website. Here, we used "/Efu6dunox0vieyelahch2eaf" for the example, but you can use any random string. You should not leave the default '/' here because that would prevent your website from working correctly.
  • Deployment directory: /private/postgres1/sureapp - If you have changed the base data directory for PostgreSQL in the script above, you should enter the correct path here.
  • Start command: /home/example/private/postgres1/ - Here, you have to replace "example" with the actual username of your hosting account.


After you create and enable the app, you will be able to use the new PostgreSQL database server.

Using the Command Line Interface of the WebApps Platform

WebApps projects can be manipulated with the sureapp command-line client. To create and start the new PostgreSQL project, you should use the following commands:

sureapp project create \
    --engine "custom" \
    --engine-version "-" \
    --release-dir "$pg_app_dir/sureapp" \
    --start-cmd "$pg_app_dir/" \
sureapp service manage --enable "$sureapp_project"
sureapp service manage --start "$sureapp_project"

After running these commands, the PostgreSQL server will be running and you will be able to use it.

Creating Databases and Roles

Once the server is running, you should set up a new user role and a database for your application. To do that, you can use the following commands over SSH:

. "/home/$USER/.bashrc"  # For $PGHOST
createuser -e -DEPRS "pgusername" createdb -e -O "pgusername" "pgdatabase"

You should replace the username and the database name in the example with the actual details you are going to set up in your application.


We create automatic system backups every 12 hours so that you don't have to worry about data loss. These backups allow you to restore your data from backup if anything goes wrong with your site.

Database engines like PostgreSQL and MySQL store data in a special way on the server, so extra care must be taken in order to ensure backup archives are consistent and usable.

For MySQL, which is integrated with our hosting environment, this is already done automatically by the Control Panel. All MySQL data is backed up at regular intervals by our system and it can be easily restored using the "Restore" page of the Control Panel.

For PostgreSQL databases running on the WebApps platform, you have to set up a backup procedure separately. The following code will create two scripts in the WebApps project, and, that will take care of backups (note that if you have used our quick setup script, these files are already created for you):

mkdir -m 0700 "$pg_app_dir/backup"

cat <<BACKUP_SH > "$pg_app_dir/"
set -e
die() { printf "%s\n" "\$*" 1>&2 && exit 1; }
"$pg_install_dir/bin/pg_dumpall" --clean --host "$pg_app_dir/run" | gzip --stdout -- > "$pg_app_dir/backup/pgdump.sql.gz" || die "PostgreSQL backup failed: $sureapp_project"
chmod 700 "$pg_app_dir/"

cat <<RESTORE_SH > "$pg_app_dir/"
set -e
die() { printf "%s\n" "\$*" 1>&2 && exit 1; }
[ -f "\$archive" ] || die "Backup file does not exist: \$archive"
zgrep -m1 "Dumped" "\$archive" || die "Backup file does not contain PostgreSQL data: \$archive"
gzip --decompress --stdout "\$archive" | "$pg_install_dir/bin/psql" --quiet --host "$pg_app_dir/run" postgres
chmod 700 "$pg_app_dir/"

After the scripts are created, you can set up a cron job that runs the script every 12 hours. This can be done on the "Cron Jobs" page of the Control Panel.

This cron job will overwrite the pgdump.sql.gz backup archive every time. This way, each new system backup will contain the newest copy of the file.

Hopefully, you won't have to use the script. If you do, however, it will drop the current data in your PostgreSQL instance and will restore the data from the pgdump.sql.gz archive.

To restore from a particular backup, you can restore the pgdump.sql.gz file from one of the system backups using the "Restore" page of the Control Panel. After that, running on the command line will restore the data from the archive into your PostgreSQL instance:


PHP Configuration

If you are going to use a PHP application with PostgreSQL, you have to update the php.ini configuration file. You can skip this step if you are not going to use PHP.

PHP can use two libraries to connect to PostgreSQL. They are the PostgreSQL database extension and the more abstract PDO driver for PostgreSQL.

You have to enable the correct PHP extension depending on the application you want to use via the  Control Panel -> PHP Settings section by following these steps:

  1. Click on the Manage button next to the domain/subdomain where you want to use ImageMagick.
  2. Check the box next to the PostgreSQL or PDO_PGSQL extension from the Extensions section.
  3. Click on the Save changes button.

The extensions can also be activated by manually adding the respective line listed below to a PHP configuration file (php.ini):

PostgreSQL extension:

extension =

PDO driver for PostgreSQL:

extension =

Instructions on how to change other PHP settings for your account are available in our Changing PHP settings article.

Enabling Remote Connections

By default, PostgreSQL is configured to listen only to local connections. There is a way to enable remote connections if you wish to connect to your PostgreSQL server from a remote server or a graphical user interface (GUI) administration tool for PostgreSQL, such as pgAdmin. In order to enable remote connections, you need to update two configuration files:

  • pgdata/postgresql.conf
  • pgdata/pg_hba.conf

 In the pgdata/postgresql.conf file, you need to edit the following lines:

listen_addresses = '' # what IP address(es) to listen on;
port = 5432                             # (change requires restart)

You need to set the "listen_address" variable to "*", and the "port" variable to the port of your WebApp. You can find the port of the WebApp in the "WebApps" section of the hosting Control Panel.

In the pgdata/pg_hba.conf file, you need to add the following line at the end:


This code will configure the PostgreSQL server to accept connections from any remote location given that they provide a valid username and password. If you wish, you can configure the server to accept connections only from a specific IP address. For instance, to allow connections from the IP address, you need to use the following line instead:

host all all md5

You should then restart the WebApp through the "WebApps" section of the hosting Control Panel, and you will be able to connect to the server from a remote location. 

Note: Updating the port in the configuration file of PostgreSQL will break the functionality of applications, such as psql, createuser, and createdb. You will still be able to use them by defining the new port in the following way:
psql -p <the WebApp port>