Aller au contenu

Service PostgreSQL

Un fichier pg_service.conf permet d'utiliser un alias nommé pour se connecter à un serveur PostgreSQL. Au lieu de stocker le nom d'hôte, le port, la nom de la base de données et plus d'informations dans le fichier de projet QGIS, ces dernières peuvent être stockées séparément. Il est possible de stocker ces informations dans un fichier pg_service.conf, cela évitant qu'elles soient stockées en clair dans le projet QGIS.

Pour en savoir plus sur les services PostgreSQL, consultez la QGIS documentation.

QField - Connexion directe

If you directly connect from QGIS to your database you can make use of a pg_service.conf file by placing it in the QField data folder. You can place your file either on the Internal Device Storage or on the SD Card Storage. You can check the path for the QField data folder in the bottom of the About QField screen in the app.

Usually the path on Android devices looks something like this: /Android/data/ch.opengis.qfield/files/QField.

Note

Unlike on *NIX systems where the file is named .pg_service.conf, the file on Android is named pg_service.conf without a leading dot sign (.).

QFieldCloud

QFieldCloud support pg_service.conf configurations too. You need to configure your PostgreSQL layers with "Offline editing" cloud action and store your service settings on QFieldCloud Project's Secrets page.

Read more how to configure PostgreSQL service in the QFieldCloud documentation.

Creating a pg_service.conf File for PostgreSQL Connection in QGIS and Secrets

Before beginning, ensure that your PostgreSQL database allows connections from QFieldCloud. Refer to Technical specs for instructions.

Setup pg_service.conf File

We first need to set up a configuration file. There are many options to organize this, read more in the PostgreSQL documentation or follow the description below.

  1. Créer un fichier de configuration:

  2. Pour Windows: Créer un fichier intitulé pg_service.conf et le stocker à l'emplacement souhaité.

  3. Pour Linux/MacOS/Unix: Créer un fichier intitulé .pg_service.conf qu'il faudra placer dans le dossier d'accueil (~).

  4. Define Connection Parameters:

Within the file, specify connection parameters for your PostgreSQL database using the following format:

[SERVICE_NAME]
host=your_host_or_ip
port=your_port
dbname=your_database_name
user=your_username
password=your_password

Replace placeholders (your_host_or_ip, your_port, your_database_name, your_username, your_password) with actual connection details and save the file.

Parameters
Parameters

Additional Configuration Steps for Windows

  1. Set Environment Variable: To ensure QGIS recognizes pg_service.conf, create an environment variable pointing to its location:

  2. Navigate to "This PC" or "My Computer" > Properties > Advanced System Settings > Environment Variables.

  3. Ajouter une nouvelle variable:
    • Nom de la variable : PGSERVICEFILE
    • Contenu de la variable : C:\Users\<YourUsername>\AppData\Roaming\postgresql\pg_service.conf (soit le chemin vers votre fichier pg_service.conf).

Alternatively, you can set environment variables directly in QGIS via Settings > Options > System > Environment. Refer to QGIS System Settings for details.

QGIS System Environment Variables
QGIS System Environment Variables

  1. Test the Connection in QGIS: Open QGIS and set up a new PostgreSQL connection using the service name defined in pg_service.conf (e.g., [MY_QGIS_DB]) in the connection details. QGIS will read configuration from pg_service.conf automatically.

  2. Ouvrir QGIS.

  3. Aller dans "Couche" > "Ajouter une couche" > "Ajouter une couche PostGIS..."
  4. Dans "Créer une nouvelle connexion PostGIS", sélectionner "Service" depuis le menu déroulant.
  5. Entrer le nom du service depuis pg_service.conf (e.g., [NINJA_DB]) dans le champs "Service".
  6. Cliquer sur "OK" pour vous connecter à votre base de données PostgreSQL en utilisant les informations de connexion contenues dans le fichier pg_service.conf.

Test your connections
Test your connections

  1. Ajouter un paramètre au Secret QFieldCloud: Naviguer jusqu'à la page des secrets du projet et copier le contenu du fichier .pg_service.conf dans le secret. Suivre Secrets pour être guidé.

Add the secret
Add the secret