PostgreSQL servicio¶
Un archivo pg_service.conf
permite utilizar un alias con nombre para una conexión al servidor PostgreSQL. En lugar de almacenar el nombre de host, el puerto, el nombre de la base de datos y más en el archivo del proyecto QGIS, estos pueden ser almacenados por separado. Incluso es posible almacenar el nombre de usuario y la contraseña en un archivo pg_service.conf
, para evitar que esto se almacene en texto claro en el Proyecto QGIS.
Lea más sobre los servicios de PostgreSQL en la documentación de QGIS.
QField - Conexión directa¶
Si se conecta directamente desde QGIS a su base de datos, puede utilizar un archivo pg_service.conf
colocándolo en la carpeta de datos de QField. Puede colocar su archivo en el almacenamiento del dispositivo interno o en el almacenamiento de la tarjeta SD. Puede verificar la ruta de la carpeta de datos de QField en la parte inferior de la pantalla Acerca de QField
en la aplicación.
Por lo general, la ruta en los dispositivos Android se ve así: /Android/data/ch.opengis.qfield/files/QField
.
Nota
A diferencia de los sistemas *NIX donde el archivo se llama .pg_service.conf
, el archivo en Android se llama pg_service.conf
sin el signo de punto inicial (.
).
QFieldCloud¶
QFieldCloud también admite configuraciones pg_service.conf
. Debe configurar sus capas de PostgreSQL con la acción en la nube "Edición sin conexión" y almacenar la configuración de su servicio en la página de secretos del proyecto QFieldCloud.
Lea más cómo configurar el servicio PostgreSQL en la documentación de 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.
-
Create a Configuration File:
-
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 (~
). -
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.
Additional Configuration Steps for Windows¶
-
Set Environment Variable: To ensure QGIS recognizes
pg_service.conf
, create an environment variable pointing to its location: -
Navigate to "This PC" or "My Computer" > Properties > Advanced System Settings > Environment Variables.
- Add a new variable:
- Variable name:
PGSERVICEFILE
- Variable value:
C:\Users\<YourUsername>\AppData\Roaming\postgresql\pg_service.conf
(or yourpg_service.conf
file path).
- Variable name:
Alternatively, you can set environment variables directly in QGIS via Settings > Options > System > Environment. Refer to QGIS System Settings for details.
-
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 frompg_service.conf
automatically. -
Open QGIS.
- Go to "Layer" > "Add Layer" > "Add PostGIS Layers..."
- In "Create a New PostGIS Connection," select "Service" from the drop-down menu.
- Enter the service name from
pg_service.conf
(e.g.,[NINJA_DB]
) in the "Service" field. - Click "OK" to connect to your PostgreSQL database using configurations from
pg_service.conf
.
- 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.