3

I've been working on setting up a PostGIS server for QField for the past week, today I almost thought I had it working but now QField keeps throwing me one error no matter what I do:

"Connection to database failed could not translate host name 'My server IP address' to address: No address associated with host name"

I have the project working wonderfully in the QGIS environment. I'm not using the built in authentication, I'm using the pg_service.conf approach. I have all my layers set to 'no action'.

At one point I though I had the authentication setup right, then I tried getting Qfield to load a Postgres hosted project and stuff stopped working.

Does anyone have any ideas of a setting I might have screwed up or a step I'm missing?

6
  • listen_addresses='*' postgresql.org/docs/current/runtime-config-connection.html is set?
    – Mapperz
    May 28, 2021 at 1:37
  • This server is hosted through a2 and so I myself have not changed this setting, they did it for me. It works from my pc, could they have excluded IPs of mobile devices? At this point all the devices I am using are connected to the same network and have the same IP address.
    – Danesadog
    May 28, 2021 at 14:52
  • Server-side listens "entry 0.0.0.0 allows listening for all IPv4 addresses"
    – Mapperz
    May 28, 2021 at 15:25
  • Ok, figured out that it was a server side problem, they hadn't fully opened up the listen address
    – Danesadog
    May 28, 2021 at 17:27
  • please answer your own question - you can accept it after a short wait time
    – Mapperz
    May 28, 2021 at 17:28

4 Answers 4

3

How I ended up fixing this was switching how I have my authentication saved. I had been using the pg_service.conf file for authentication, this worked great on my pc but for some reason the same file on my android did not work, it just couldn't read it right. I switched to saving the authentication in the project and shipped that to the tablet, this is technically less secure because anyone who copies the project folder now has that password, but for my purposes this works fine.

Sorry I took forever to get back to this.

4
  • Is it possible for you to go into more detail on how to do this? I'm having the same issue and @mathias isnt clear on how to do this for android. Sep 12, 2021 at 22:17
  • Could you explain to me what your issue is? Are you having trouble with making the pg_service.conf file or with using that file for qfield?
    – Danesadog
    Sep 15, 2021 at 20:47
  • How did you switch saving the authentication to the project? What setting or how do I go about doing that step? Sep 16, 2021 at 21:05
  • are you still struggling with this? sorry ive just been off awhile
    – Danesadog
    Oct 15, 2021 at 22:50
2

I found the proper solution for this problem.

"The service file should be saved in Unix format in order to work. One way to do it is to open it with Notepad++ and Edit ► EOL Conversion ► UNIX Format ► File save." https://docs.qgis.org/3.16/en/docs/user_manual/managing_data_source/opening_data.html#pg-service-file

That was all it took to get it to work. hope this helps someone.

1

Since the version 2.0 the pg_service.conf file must be placed in : Android\data\ch.opengis.qfield\files\QField and no more directly in a directory named QField in the main folder.

1
  • I was using xml auth file in version 1.x of Qfield. After update to 2.x this approach it did not work anymore (I saved it to the correct folder in: Android\data\ch.opengis.qfield\files\QField\auth). I solved my problem by saving connection details for PostGIS directly to project file - basic authentication with store checked. This is not ideal as the password is stored in project file.
    – Trnovstyle
    Apr 6, 2022 at 19:38
0

This is how I connect QField to PostGIS on Android phone using the connection service file:

First, on Windows PC, create file .pg_service.conf to store login information. My file looks like this:

[my_service_name]
host=xxx.xxx.xxx.xxx
port=5432
dbname=my_database_name
user=my_username
password=my_password
sslmode=require

Use Notepad++ to convert end-of-line characters to Unix style as described in Qgis documentation and save in C:\Users\MyUsername\AppData\Roaming\postgresql - you may have to create the folder postgresql yourself.

Second, open Qgis and creat a new project, create a new connection to PostgreSQL using menu layer > Add layer > Add PostGIS Layer > new. I filled out the opened form as follow:

The name field could be whatever name you want to call your new connection and the service field should be the same as the first line in your .pg_service.conf file, minus the brackets - in this case it should be my_service_name

At the Data Source Manager | PostgreSQL form, choose your newly created connection and click Connect - you should be able to connect to your Postgis database and see your layers listed - add any layer you want.

Save your project using .qgs format - you are about to achieve your aim of working with Postgis layers on Android phone using QField.

Create a copy of your .pg_service.conf, rename it to pg_service.conf (remove the first dot in the name) and ship to your Android phone - remember to put it in the folder Android\data\ch.opengis.qfield\files\QField.

Finally, ship the above .qgs project to your phone, open it with QField and enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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