PostgreSQL - Tutorial - Install Wagtail and connect it to OVHcloud Managed PostgreSQL service

Find out how to install Wagtail and connect it to an OVHcloud Managed PostgreSQL service

Last updated 11th February 2022

Objective

Wagtail is a free and open source content management system (CMS) written in Python. It is popular amongst websites using the Django framework.

In this tutorial, we are going to show you how to install Wagtail and how to connect it to the OVHcloud managed PostgreSQL database service.

OVHcloud provides services for which you are responsible for their configuration and management. You are therefore responsible for their proper functioning.

This tutorial is designed to help you as much as possible with common tasks. If you are having difficulty performing these actions, please contact a specialized service provider and/or discuss it with our community of users on https://community.ovh.com/en/. OVHcloud can't provide you with technical support in this regard.

Requirements

  • Access to the OVHcloud Control Panel
  • A Public Cloud project in your OVHcloud account.
  • An up and running Public Cloud Database for PostgreSQL.
  • A Python environment with a stable version and public network connectivity (Internet). This tutorial was made using Python 3.9.7.

Wagtail supports Python 3.7, 3.8, 3.9 and 3.10.

Configure your PostgreSQL instance to accept incoming connections

Before making a connection, we need to verify that our PostgreSQL instance is correctly configured.

Log in to your OVHcloud Control Panel and open your Public Cloud project. Click on Databases in the left-hand navigation bar and select your PostgreSQL instance.

Step 1: Verify your user roles and password

Select the Users tab. Verify that you have a user with sufficient rights and a configured password. If you don't remember the user's password, you can either create a new user or regenerate the password of an existing user. Be careful! By doing so you will need to update all the places where you already use this user/password pair.

This first user avnadmin comes with the following privileges:

  LOGIN
  NOSUPERUSER
  INHERIT
  CREATEDB
  CREATEROLE
  REPLICATION

We rely on official PostgreSQL roles and privileges. You can manage them yourself via CLI or code. So far, user grants and privileges management are not supported via the OVHcloud Control Panel or the OVHcloud API.

Please read the official PostgreSQL documentation to select the right roles for your use case.

In our example, we will simply reset the avnadmin password.

Once created or updated, the user has to be ready and have the status "Enabled" in the Control Panel.

User ready

Step 2: Authorise incoming connections from the PostgreSQL client

In this step, select the Authorised IP's tab (Access Control List). By default, a Public Cloud Database does not accept any form of connection from the outside world. This way we can help prevent intrusive connection attempts.

Click to authorise a new IP, and enter the previously found IP of your Python environment. In our case we will enter 109.190.200.59.

Add an IP

If you want to allow any connections from the outside, you can enter the IP 0.0.0.0/0. Please use it carefully. Every IP will be authorised.

Before installing Wagtail, it is necessary to install the libjpeg and zlib libraries, which provide support for working with JPEG, PNG and GIF images (via the Python Pillow library).

Instructions

Create and activate a virtual environment

$ python3 -m venv mysite/env
$ source mysite/env/bin/activate

If you’re using version control (e.g. git), mysite will be the directory for your project. The env directory inside of it should be excluded from any version control.

Install Wagtail

Please follow the official documentation to install Wagtail.

Below, you find a resume of the installation process

Use pip, which is packaged with Python, to install Wagtail and its dependencies:

$ pip install wagtail

Generate your site

Wagtail provides a start command similar to django-admin startproject. Running wagtail start mysite in your project will generate a new mysite folder with a few Wagtail-specific extras, including the required project settings, a “home” app with a blank HomePage model and basic templates, and a sample “search” app.

Because the folder mysite was already created by venv, run wagtail start with an additional argument to specify the destination directory:

$ wagtail start mysite mysite

Install project dependencies

$ cd mysite
$ pip install -r requirements.txt

Create the database

If you keep wagtail on default settings, a local SQLlite database will be used inside the project directory. It’s fine for a PoC but not recommended for production, for multiple reasons (performance, security, resiliency, ...). Here we will configure Wagtail to use a Public Cloud Database for PostgreSQL as a backend.

Before creating the database, let's edit the mysite/mysite/settings/base.py file and adapt the connection parameters to the database.

The useful parameters, available in the OVHcloud Control Panel are:

  • The db Host, the db Port
    • Get them from the "General Information tab".
  • The db Name
    • Get it from the "Databases" tab, usually "defaultdb".
  • The db User
    • Get it from the "Users" tab, usually "avnadmin".
  • The db Password
    • Get it after you reset it.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': <db_name>,
        'USER': <username>,
        'PASSWORD': <password>,
        'HOST': <host>,
        'PORT': <port>,
        'OPTIONS': {
          'sslmode': 'require'
        }
    }
}

Then create the database:

$ python manage.py migrate

This command ensures that the tables in your database are matched to the models in your project. Every time you alter your model (e.g. you may add a field to a model) you will need to run this command in order to update the database.

Create an admin user

When logged into the admin site, a superuser has full permissions and is able to view/create/manage the database.

$ python manage.py createsuperuser

Start the server

$ python manage.py runserver

If everything worked, http://127.0.0.1:8000 will show you a welcome page:

Wagtail welcome page

You can now access the administrative area at http://127.0.0.1:8000/admin:

Wagtail admin page

Cleaning up

To clean your Wagtail, make sure it is closed by pressing CTRL+C in the terminal you used to launch it, then delete your installation folder.

rm -rf /home/my/app/path/my-wagtail/

To clean your PostgreSQL, use the OVHcloud Control Panel to delete your managed PostgreSQL service:

delete service menu

We want your feedback!

We would love to help answer questions and appreciate any feedback you may have.

Are you on Discord? Connect to our channel at https://discord.gg/ovhcloud and interact directly with the team that builds our databases service!


¿Le ha resultado útil esta guía?

Si lo desea, también puede enviarnos sus sugerencias para ayudarnos a mejorar nuestra documentación.

Imágenes, contenido, estructura...: ayúdenos a mejorar nuestra documentación con sus sugerencias.

No podemos tratar sus solicitudes de asistencia a través de este formulario. Para ello, haga clic en "Crear un tíquet" .

¡Gracias! Tendremos en cuenta su opinión.


Otras guías que podrían interesarle...

OVHcloud Community

¡Acceda al espacio de la OVHcloud Community! Resuelva sus dudas, busque información, publique contenido e interactúe con otros miembros de la comunidad.

Discuss with the OVHcloud community

A partir del 1 de enero de 2015, con arreglo a la Directiva 2006/112/CE modificada, los precios IVA incluido pueden variar según el país de residencia del cliente (por defecto, los precios con IVA incluyen el IVA español vigente).