Search code examples
databasepostgresqlserverprojectqgis

Can QGIS server read projects from database


I have QGIS server working. The standard functionality is to add ?map=path/to/projectfile.qgs to the server URL. So you can dynamically switch between projects in your webapplication. Now I make my projects and upload my projectfile to the server to get them working in my application. That works fine.

But I can also store the project in the database. It would be much nicer if I could tell my application to use a project from my database. Skipping the cumbersome file update procedure.

Researching this I came across this info from the QGis documentation: https://docs.qgis.org/3.16/en/docs/server_manual/config.html in the section: 5.2. Environment variables I see the following info:

QGIS_PROJECT_FILE

The .qgs or .qgz project file, normally passed as a parameter in the query string (with MAP), you can also set it as an environment variable (for example by using mod_rewrite Apache module). postgresql://localhost:5432?sslmode=disable&dbname=mydb&schema=myschema&project=myproject

So you can point to a projectfile in the database for the default projectfile. But that's not what I want. I want to do it dynamically.

What I want is to have something like ?map=projectfile_in_my_database. And specifiy in my conf / environment on the server where these are stored in de DB.

Is this possible?


Solution

  • You can use project files stored in a database (in my case it is postgres). How I did it.

    1. I created a pg_service file on a home directory which contains database connection credentials and let us to connect to database just specifying service name, for example using psql you can connect psql service=myservicename and set fastcgi params on nginx fastcgi_param PGSERVICEFILE /home/qgis/.pg_service.conf;
    2. I connected from qgis desktop to postgres database specifying the service name which I set in service file.
    3. Saved project file in database. By doing like this project file will contain database connection via service name.
    4. Set fastcgi_param for project name
    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        gzip           off;
        include        fastcgi_params;
        fastcgi_param  QGIS_PROJECT_FILE postgresql:///?service=myservicename&schema=public&project=testproject;
        fastcgi_pass   unix:/var/run/fcgiwrap.socket;;
        }
    
    1. Default qgis project you can set in frontend of your web application via url, that is not difficult.