我得到:没有函数与psycopg2执行poytgis函数时的给定名称和参数类型匹配

发布于 2025-01-18 18:39:26 字数 1917 浏览 3 评论 0原文

我正在尝试使用 psycopg2 执行来自扩展 (postgis) 的函数。

import psycopg2

AFRICA = "africa"
ANTARCTICA = "antarctica"
ASIA = "asia"
AUSTRALIA_OCEANIA = "australia-oceania"
CENTRAL_AMERICA = "central-america"
EUROPE = "europe"
NORTH_AMERICA = "north-america"
SOUTH_AMERICA = "south-america"

SCHEMAS = [AFRICA, ANTARCTICA, ASIA, AUSTRALIA_OCEANIA,
           CENTRAL_AMERICA, EUROPE, NORTH_AMERICA, SOUTH_AMERICA]

def createCentroidTableFromPolygon(fromTable, toTable):
    return f"""
        CREATE TABLE IF NOT EXISTS {toTable} AS
        SELECT ST_Centroid(geom) AS geom, way_id, osm_type, name FROM {fromTable};
    """

for schema in SCHEMAS:    
    conn = psycopg2.connect(
        host='localhost',
        database='world',
        user="postgres",
        password="postgres",
        port=5432,
        options=f"-c search_path={schema}"
    )

    for i, table in enumerate(TABLES):
        # https://stackoverflow.com/questions/57116846/run-postgresql-functions-in-python-and-gets-error
        with conn:
            with conn.cursor() as cursor:
                # this works!
                cursor.execute(f"""SELECT * FROM {table} LIMIT 10""")
                print(cursor.fetchall())

                # this throws an error
                cursor.execute(createCentroidTableFromPolygon(
                table, FROM_POLY_TABLES[i]))

这给了我

psycopg2.errors.UndefinedFunction:函数 st_centroid(public.geometry) 不存在 第 3 行:选择 ST_Centroid(geom) AS geom, way_id, osm_type, name...
提示:没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换。

不过,扩展postgis已安装在数据库世界上。

SELECT * FROM pg_extension;

扩展 postgres

当我尝试在 pgAdmin 中运行此函数时,它工作没有问题......

I am trying to execute a function which comes from an extension (postgis) with psycopg2.

import psycopg2

AFRICA = "africa"
ANTARCTICA = "antarctica"
ASIA = "asia"
AUSTRALIA_OCEANIA = "australia-oceania"
CENTRAL_AMERICA = "central-america"
EUROPE = "europe"
NORTH_AMERICA = "north-america"
SOUTH_AMERICA = "south-america"

SCHEMAS = [AFRICA, ANTARCTICA, ASIA, AUSTRALIA_OCEANIA,
           CENTRAL_AMERICA, EUROPE, NORTH_AMERICA, SOUTH_AMERICA]

def createCentroidTableFromPolygon(fromTable, toTable):
    return f"""
        CREATE TABLE IF NOT EXISTS {toTable} AS
        SELECT ST_Centroid(geom) AS geom, way_id, osm_type, name FROM {fromTable};
    """

for schema in SCHEMAS:    
    conn = psycopg2.connect(
        host='localhost',
        database='world',
        user="postgres",
        password="postgres",
        port=5432,
        options=f"-c search_path={schema}"
    )

    for i, table in enumerate(TABLES):
        # https://stackoverflow.com/questions/57116846/run-postgresql-functions-in-python-and-gets-error
        with conn:
            with conn.cursor() as cursor:
                # this works!
                cursor.execute(f"""SELECT * FROM {table} LIMIT 10""")
                print(cursor.fetchall())

                # this throws an error
                cursor.execute(createCentroidTableFromPolygon(
                table, FROM_POLY_TABLES[i]))

This gives me

psycopg2.errors.UndefinedFunction: function st_centroid(public.geometry) does not exist
LINE 3: SELECT ST_Centroid(geom) AS geom, way_id, osm_type, name...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

The extension postgis is installe on the database world though.

SELECT * FROM pg_extension;

extensions postgres

When I try to run this function in pgAdmin, it works without a problem...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2025-01-25 18:39:26

这是解决方案https://gis.stackexchange.com/questions/324386/ psycopg2-programmingerror-function-does-not-exist

Andre Silva:

对 PostGIS 函数的所有调用都必须是模式限定的:schema_name.function (来源)。
要绕过每次使用 PostGIS 函数时写入架构,请将 PostGIS 所在的架构(可能是公共的)映射到 search_path(请参阅此处)。作为管理员:

ALTER DATABASE <database_name> SET search_path TO schema1,schema2;

此外,请确保私有用户在数据库中拥有必要的权限以继续分析(请查看这里)。

我这样解决了它:

def createCentroidTableFromPolygon(fromTable, toTable):
    return f"""
    CREATE TABLE IF NOT EXISTS {toTable} AS
    SELECT public.ST_Centroid(geom) AS geom, way_id, osm_type, name FROM {fromTable};
    """

This is the solution https://gis.stackexchange.com/questions/324386/psycopg2-programmingerror-function-does-not-exist

Andre Silva:

All calls to PostGIS functions must be schema qualified: schema_name.function (source).
To bypass writing the schema every time a PostGIS function is used, map the schema where PostGIS is (probably public) to the search_path (see here). As admin:

ALTER DATABASE <database_name> SET search_path TO schema1,schema2;

Moreover, make sure the private user has the necessary privileges in the database to proceed with the analysis (take a look here).

I solved it like this:

def createCentroidTableFromPolygon(fromTable, toTable):
    return f"""
    CREATE TABLE IF NOT EXISTS {toTable} AS
    SELECT public.ST_Centroid(geom) AS geom, way_id, osm_type, name FROM {fromTable};
    """
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文