Running a PostgreSQL instanceIn 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 SetupIf 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:
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 SetupThe 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
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):
Now, you have to configure the PostgreSQL server to listen for requests using a UNIX socket at the correct location in your account:
To make starting the PostgreSQL service easier, the following commands will create a startup script:
In order to use various PostgreSQL management tools such as
Custom WebApps ProjectTo run the database server, you have to set up a PostgreSQL service. There are two ways to do this:
The following sections describe how to do this. On the WebApps Page of the Control PanelIf you choose the Control Panel way, you should create a new app with the following settings:
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 PlatformWebApps projects can be manipulated with the
After running these commands, the PostgreSQL server will be running and you will be able to use it. Creating Databases and RolesOnce 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:
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. BackupsWe 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,
After the scripts are created, you can set up a cron job that runs the This cron job will overwrite the Hopefully, you won't have to use the To restore from a particular backup, you can restore the
PHP ConfigurationIf you are going to use a PHP application with PostgreSQL, you have to update the 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:
The extensions can also be activated by manually adding the respective line listed below to a PHP configuration file (php.ini): PostgreSQL extension:
PDO driver for PostgreSQL:
Enabling Remote ConnectionsBy 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:
In the pgdata/postgresql.conf file, you need to edit the following lines: listen_addresses = '' # what IP address(es) to listen on; 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: host all all 0.0.0.0/0 md5 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 123.123.123.123 IP address, you need to use the following line instead: host all all 123.123.123.123/32 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: |