Vai al contenuto

PostgreSQL service

Un file pg_service.conf permette di usasre un alias per una connessione a un server PostgreSQL. Invece di salvare hostname, porta, nome del database e altro nel progetto QGIS, questi dati possono essere salvati separatamente. È anche possibile salvare username e password nel file pg_service.conf per evitare di doverli salvare come testo in chiaro nel progetto QGIS.

Scopri di più a proposito dei PostgreSQL services nella documentazione QGIS.

QField - Connessione diretta

Se ti connetti direttamente a un database da QGIS, puoi usare un file pg_service.conf mettendolo direttamente nella directory dei dati di QField. Puoi mettere il tuo file sia nella memoria interna del dispositivo sia nella scheda SD. Puoi verificare il percorso della directory dei dati di QField in fondo alla schermata About QField nell'app.

Di solito il percorso su Android assomiglia a qualcosa del genere: /Android/data/ch.opengis.qfield/files/QField.

Nota

A differenza dei sistemi *NIX dove il file si chiama .pg_service.conf, su android si chiama pg_service.conf senza il punto iniziale (.).

QFieldCloud

Anche QFieldCloud supporta configurazioni pg_service.conf. Devi configurare i tuoi layer con azione "Offline editing" e salvare la configurazione del progetto nella pagina dei segreti del progetto su QFieldCloud.

Ottieni maggiori informazioni su come configurare servizi PostgreSQL nella documentazione di QFieldCloud.

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. Create a Configuration File:

  2. On Windows: Create a file named pg_service.conf and store it in a convenient location.

  3. On Linux/MacOS/Unix: Create a file named .pg_service.conf in your home folder (~).

  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. Add a new variable:
    • Variable name: PGSERVICEFILE
    • Variable value: C:\Users\<YourUsername>\AppData\Roaming\postgresql\pg_service.conf (or your pg_service.conf file path).

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. Open QGIS.

  3. Go to "Layer" > "Add Layer" > "Add PostGIS Layers..."
  4. In "Create a New PostGIS Connection," select "Service" from the drop-down menu.
  5. Enter the service name from pg_service.conf (e.g., [NINJA_DB]) in the "Service" field.
  6. Click "OK" to connect to your PostgreSQL database using configurations from pg_service.conf.

Test your connections
Test your connections

  1. Add Parameter to QFieldCloud Secrets: Navigate to the project's secrets page and copy the service directly from .pg_service.conf to the secret. Follow Secrets for guidance.

Add the secret
Add the secret