我得到:没有函数与psycopg2执行poytgis函数时的给定名称和参数类型匹配
我正在尝试使用 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;
当我尝试在 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;
When I try to run this function in pgAdmin
, it works without a problem...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是解决方案https://gis.stackexchange.com/questions/324386/ psycopg2-programmingerror-function-does-not-exist
Andre Silva:
对 PostGIS 函数的所有调用都必须是模式限定的:schema_name.function (来源)。
要绕过每次使用 PostGIS 函数时写入架构,请将 PostGIS 所在的架构(可能是公共的)映射到 search_path(请参阅此处)。作为管理员:
此外,请确保私有用户在数据库中拥有必要的权限以继续分析(请查看这里)。
我这样解决了它:
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:
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: