The original version of this document is located at https://docs.qfield.org/how-to/project-setup/pg-service
Working with databases can be much easier when working in a multi-user environment or when working in a variety of projects that require more complex data structures.
With QField you can easily work within a database but there are a few steps that need to be taken before the smoothless fieldwork can begin.
This page goes through the different options and gives step-by-step instructions on how this can be achieved.
In QGIS there are two options in which you can connect to your database.
You can create a PostgreSQL connection directly inside QGIS.
NOTE: We do not recommend this option due to data safety reasons.
It is not safe to store your database credentials in the QGIS Project File.
Workflow
If you have added the access credentials in the “Authentication” section as described above, QField will be able to directly start editing and adding new features.

It is possible to connect to postgreSQL via a service using a service file.
A service is stored inside the pg_service.conf file where all the required information to access your database is stored.
Instead of storing the hostname, port, database name and more into the QGIS Project file, these can be stored separately.
Using services is very useful, when you work with multiple database connections during your everyday work to quickly change and connect to the different instances.
Read more about PostgreSQL services in the QGIS documentation.
There exists a useful plugin, which supports in the easy creation and configuration of the service file - the PG Service Parser Plugin.
We recommend using this plugin and will show you below how to use it.
Workflow
🖥️ Desktop Preparation
If you never set-up a service file before, the plugin will automatically create a new file in a proper default directory, which ensures QGIS will be able to read it.

Click on “Create file at default location” and leave the default name.
Now you can configure the database information which will be stored as a PG service.
Create service

Connection to PostGIS via service
The new QGIS Connection will appear in your browser, listed under the PostgreSQL item.
If you have connected to PostGIS using the PG Service file and, it is also necessary to add the service file either on your mobile device directly or within QFieldCloud as a “Secret”.
If you copy your projects via cable to your device, you will also have to copy the service file to the right directory on your device.
Generally, the QField directory for Android can be found under /Android/data/ch.opengis.qfield/files/QField.
Note
1. Due to the restrictions of Android, you will only be able to access the directory when being connected via cable to the computer.
The only other possible option you have is to set your device to root (not recommended).
2. 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 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.
pg_service.conf File for PostgreSQL Connection in QGIS and SecretsBefore beginning, ensure that your PostgreSQL database allows connections from QFieldCloud.
Refer to Technical specs for instructions.
pg_service.conf FileWe 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.
On Windows:
Create a file named pg_service.conf and store it in a convenient location.
On Linux/MacOS/Unix:
Create a file named .pg_service.conf in your home folder (~).
Within the file, specify connection parameters for your PostgreSQL database using the following format:
ini
[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.
Set Environment Variable:
To ensure QGIS recognizes pg_service.conf, create an environment variable pointing to its location:
PGSERVICEFILEC:\Users\<YourUsername>\AppData\Roaming\postgresql\pg_service.conf (or your pg_service.conf file path).
Set Environment Variable in QGIS

Refer to QGIS System Settings for details.
It is possible for you to use client certificates to verify the identity while connecting to a PostgreSQL server by defining additional parameters in your pg_service.conf, namely sslcert, sslkey, and sslrootcet.
ini
[SERVICE_NAME]
host=your_host_or_ip
port=your_port
dbname=your_database_name
user=your_username
password=your_password
sslcert=client.crt
sslkey=client.key
sslrootcert=server.crt
These parameters must point to valid certificate and key files and placed directly alongside your pg_service.conf file within the QField data folder. For more information on this identification method, refer to the PostgreSQL documentation.