I have the same problem, bellow my code if some one can help me please.
from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
def run_script(iface):
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "Base_test", "user", "****")
uri.setDataSource("public", "mytable", "geom","")
vlayer = QgsVectorLayer(uri.uri(), "mytable", "user")
QgsMapLayerRegistry.instance().addMapLayers([layer])
When I run this code I haven't un error but i see nothing on my QGIS interface.
I'm using, QGIS 3.12.
from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
from qgis.utils import *
import processing
def run_script(iface):
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "Base_test", "Nassim", "*****")
uri.setDataSource("public", "BPE", "geom")
layer = QgsVectorLayer(uri.uri(), "BPE", "Nassim")
QgsProject.instance().addMapLayer(layer)
This is my code whith the solution given by @Vincent Bré, but it still doesn't work

run_script(iface)? It's because all the code you show us is only declaring the function but you need to call it to work – ThomasG77 Jun 19 '20 at 11:07