You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行osm2pgsql时提示角色'mapper'不存在的问题求助

解决osm2pgsql连接PostgreSQL时"role 'mapper' does not exist"的问题

问题背景

我按步骤搭建地理空间数据库:

  • 安装PostgreSQL
  • 执行createuser mapper创建角色
  • 执行createdb -E UTF8 -O mapper gis创建gis数据库
  • 运行以下命令完成数据库配置:
    psql -d gis -f /usr/share/postgresql/contrib/postgis-2.1/postgis.sql
    psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO mapper;"
    psql -d gis -U mapper -f /usr/share/postgresql/contrib/postgis-2.1/spatial_ref_sys.sql
    

但执行osm2pgsql导入命令时出错:

ERROR: Connecting to database failed: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "mapper" does not exist

通过\du查看角色,确认mapper存在:

Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 mapper    |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

尝试执行CREATE ROLE mapper LOGIN;和CREATE ROLE "mapper" LOGIN;添加登录权限,系统提示角色已存在。

解决方法

1. 为mapper角色添加LOGIN权限

从\du输出可见,mapper角色缺少登录权限(Attributes列为空)。以postgres超级用户身份执行以下命令补全权限:

psql -U postgres -c "ALTER ROLE mapper LOGIN;"

执行后再次运行\du,mapper的Attributes列应显示Login。

2. 显式指定数据库连接参数

osm2pgsql可能未使用默认连接配置,尝试显式指定主机和端口:

osm2pgsql -s -d gis -C 2000 --number-processes 3 -U mapper -h localhost -p 5432 ./data/cameroon-latest.osm.bz2

3. 检查pg_hba.conf的身份验证规则

确保PostgreSQL允许mapper用户本地登录:

  1. 找到pg_hba.conf文件(通常路径为/etc/postgresql/<你的PostgreSQL版本>/main/pg_hba.conf)
  2. 添加或修改以下配置行:
    local   all             mapper                                     trust
    
  3. 重启PostgreSQL服务使配置生效:
    sudo systemctl restart postgresql
    

4. 确认执行osm2pgsql的用户上下文

避免以root用户执行osm2pgsql,可能引发身份验证异常。若需指定非默认socket路径,可添加--socket参数:

osm2pgsql -s -d gis -C 2000 --number-processes 3 -U mapper --socket /var/run/postgresql/ ./data/cameroon-latest.osm.bz2

内容的提问来源于stack exchange,提问作者SIU ABONI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 12:50:17