Is it possible to use an environment variable in qgis project file (*.qgs)? For example :

<datasource>dbname='QBASE' host= port=5432 user=USERNAME sslmode=disable key='gid' srid=0 type=MultiPolygon table="public"."geo_zone" (geom) sql=</datasource>

I need set value of environment variable "USERNAME" to value of "user".

1 Answer 1


Unfortunately I am not able to give you an answer to the original question. I cannot think of an idea to work the way you proposed.

I think however that it is possible to solve your problem with a different approach, in particular pg_service.

In the .qgs file you can connect to the database by means of the service parameter. instead of specifying host, port and user individually.

In the .qgs file it should say service='pg_qbase' but you can define that from the postgres connection UI in QGIS with the field named service.

While the pg_service.conf file should contain the following

user={this part is unique per machine/user}

If you want to put the password into a separate file, there is also a .pgpass file which avoids having to ship passwords in pg_service.conf or the .qgs file.

With this approach you are able to

  • define database access rights per user
  • while sharing one .qgs project file
  • and referencing the same database from multiple .qgs files
  • Matthias Kuhn, this good method, but my idea is show form with credentials with typed name. This makes it possible to tape any name besides from pg_service. But thanks, probably I will move on this method.
    – johnzet
    Sep 14, 2015 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.