Vai al contenuto

PostgreSQL service

A pg_service.conf file allows to use an named alias for a PostgreSQL server connection. Instead of storing hostname, port, database name and more into the QGIS Project file, these can be stored separately. It is even possible to store username and password in a pg_service.conf file, to avoid having this stored in clear text in the QGIS Project.

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

QField - Connessione diretta

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.

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

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.

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

Creare un File pg_service.conf per la Connessione PostgreSQL in QGIS e Secreti

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

Configurazione File pg_service.conf

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. Crea un File di Configurazione:

  2. In Windows: Crea un file chiamato pg_service.conf e memorizzalo in un luogo facilmente accessibile.

  3. Su Linux/MacOS/Unix: Crea un file chiamato .pg_service.conf nella tua cartella home (~).

  4. Definisci i Parametri di Connessione:

All'interno del file, specificare i parametri di connessione per il database PostgreSQL utilizzando il seguente formato:

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

Sostituire i segnaposto (your_host_or_ip, your_port, your_database_name, your_username, your_password) con i dettagli della connessione e salvare il file.

Parameters
Parameters

Ulteriori Passaggi di Configurazione per Windows

  1. Imposta la Variabile d'Ambiente: Per garantire che QGIS riconosca pg_service.conf, creare una variabile d'ambiente che punti alla sua posizione:

  2. Accedere a "Questo PC" o "Il mio computer" > Proprietà > Impostazioni di sistema avanzate > Variabili di ambiente.

  3. Aggiungi una nuova variabile:
    • Nome variabile: PGSERVICEFILE
    • Valore della variabile: C:\Users\<YourUsername>\AppData\Roaming\postgresql\pg_service.conf (o il tuo percorso del file 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. Apri QGIS.

  3. Vai a "Layer" > "Aggiungi Layer" > "Aggiungi Layers PostGIS..."
  4. In "Crea una nuova connessione PostGIS", seleziona "Servizio" dal menu a discesa.
  5. Inserisci il nome del servizio da pg_service.conf (es., [NINJA_DB]) nel campo "Service".
  6. Clicca su "OK" per connettersi al database PostgreSQL usando le configurazioni di pg_service.conf.

Testare le tue connessioni
Testare le tue connessioni

  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