Zum Inhalt

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.

Lesen Sie mehr über PostgreSQL-Dienste in der QGIS-Dokumentation.

QField - Direkte Verbindung

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.

Normalerweise sieht der Pfad auf Android-Geräten in etwa so aus: /Android/data/ch.opengis.qfield/files/QField

Anmerkung

Anders als auf *NIX-Systemen, bei denen die Datei .pg_service.conf benannt ist, wird die Datei auf Android pg_service.conf ohne führendes Punktzeichen (.) benannt.

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.

Lesen Sie mehr darüber, wie Sie den PostgreSQL-Dienst konfigurieren in der QFieldCloud-Dokumentation.

Erstellen einer pg_service.conf Datei für eine PostgreSQL-Verbindung in QGIS und Secrets

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

Setup pg_service.conf Datei

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. Erstellen Sie eine Konfigurationsdatei:

  2. Unter Windows: Erstellen Sie eine Datei mit dem Namen pg_service.conf und speichern Sie diese an einem geeigneten Ort.

  3. Unter Linux/MacOS/Unix: Erstellen Sie eine Datei mit dem Namen .pg_service.conf in Ihrem Benutzerordner (~).

  4. Verbindungsparameter definieren:

Geben Sie in der Datei die Verbindungsparameter für Ihre PostgreSQL-Datenbank im folgenden Format an:

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

Ersetzen Sie die Platzhalter (your_host_or_ip, your_port, your_database_name, your_username, your_password) durch die tatsächlichen Verbindungsdetails und speichern Sie die Datei.

Parameter
Parameter

Zusätzliche Konfigurationsschritte für Windows

  1. Umgebungsvariable setzen: Um sicherzustellen, dass QGIS die pg_service.conf Datei erkennt, erstellen Sie eine Umgebungsvariable, die auf ihren Speicherort zeigt

  2. Navigieren Sie zu "Dieser PC" oder "Arbeitsplatz" > Eigenschaften > Erweiterte Systemeinstellungen > Umgebungsvariablen.

  3. Fügen Sie eine neue Variable hinzu:
    • Variablen-Name: PGSERVICEFILE
    • Variablen-Wert: C:\Users\<YourUsername>\AppData\Roaming\postgresql\pg_service.conf (oder Ihr pg_service.conf Dateipfad).

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

QGIS Systemumgebungsvariable
QGIS Systemumgebungsvariable

  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. Öffnen Sie QGIS.

  3. Navigieren Sie zu "Layer" > " Layer hinzufügen" > "PostGIS Layer hinzufügen..."
  4. Wählen Sie unter "Neue PostGIS-Verbindung erstellen" die Option "Service" aus dem Dropdown-Menü aus.
  5. Geben Sie den Dienstnamen aus der pg_service.conf Datei (z. B. [NINJA_DB]) in das Feld "Dienst" ein.
  6. Klicken Sie auf "OK", um eine Verbindung zu Ihrer PostgreSQL-Datenbank herzustellen, indem Sie die Konfigurationen aus der pg_service.conf Datei verwenden.

Testen Sie Ihre Verbindung
Testen Sie Ihre Verbindung

  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.

Secret hinzufügen
Secret hinzufügen