This thread is old but there are no further discussions about this topic.
My setting is: qgis(-server) running in a container, local conda create env qgis.
I think its not related to the platform. In a conda env with qgis (3.30) I have the same redcuced provider list as stated in comments before.
I also tried to set the setPrefixPath to env/qgis/share/qgis or env/qgis/lib where the *postgres*.so is located but no luck.
Maybe it works if env/qgis/lib is added to sys.path initially, but untested.
So a solution can be to read the credentials from the layer by reading the qgs xml file with e.g. xmltodict and psycopg2 to accesss the layer.
If processing algs should be used then the layer needs to be recreated as a QgsVectorLayer from the select query.
But the problem of missing postgres provider in the qgis package by installing with
conda create env qgis
remains.
Update
i wanted to use processing where i had to call Processing.initialize(). I did call it before initQgis(). As stated here the providers should load with that command. But if Processing.initialize() is called before not all providers, e.g. postgres are loaded.
Well in the docs it is stated after initQgis():
Write your code here to load some layers, use processing
algorithms, etc.
that was not clear to me as i thought calling Processing.initialize() where processing providers QgsProcessingProvider are appended to QgsApplication.processingRegistry() does not affect QgsApplication().initQgis().
import os
from qgis.core import QgsApplication,QgsProviderRegistry
from processing.core.Processing import Processing
os.environ["QT_QPA_PLATFORM"] = "offscreen"
def run():
qgs = QgsApplication([], False)
# # this does not work
# Processing.initialize()
qgs.initQgis()
Processing.initialize()
print("provider path: ", QgsProviderRegistry.instance().libraryDirectory().path())
print("provider list: ",QgsProviderRegistry.instance().providerList())
run()