GitHub Action中配置PostGIS后报错geometry类型不存在,求解决
解决GitHub Action中PostGIS的"geometry类型不存在"错误
可能的原因
- 数据库尚未完全就绪时就执行了创建PostGIS扩展的命令,导致扩展未成功加载
- 执行扩展创建命令的用户缺少必要权限(虽
POSTGRES_USER默认会被设为超级用户,但存在例外场景) - Laravel迁移执行时,目标数据库的PostGIS扩展未正确初始化
解决方案
1. 等待数据库完全就绪后再创建扩展
修改"Enable PostGIS extension"步骤,先确认数据库可正常连接,再执行扩展创建命令:
- name: Enable PostGIS extension run: | # 轮询等待数据库就绪 until PGPASSWORD=password psql -h 127.0.0.1 -U postgres_user -d postgres_tests -c "SELECT 1" > /dev/null 2>&1; do echo "Waiting for database to be ready..." sleep 2 done # 创建PostGIS扩展 PGPASSWORD=password psql -h 127.0.0.1 -U postgres_user -d postgres_tests -c "CREATE EXTENSION IF NOT EXISTS postgis;" env: PGPORT: 5432
2. 确保用户拥有超级用户权限(可选)
如果上述步骤仍失败,可先为操作用户赋予超级用户权限,再创建扩展:
- name: Enable PostGIS extension run: | until PGPASSWORD=password psql -h 127.0.0.1 -U postgres_user -d postgres_tests -c "SELECT 1" > /dev/null 2>&1; do echo "Waiting for database to be ready..." sleep 2 done # 赋予超级用户权限 PGPASSWORD=password psql -h 127.0.0.1 -U postgres_user -d postgres_tests -c "ALTER USER postgres_user WITH SUPERUSER;" # 创建PostGIS扩展 PGPASSWORD=password psql -h 127.0.0.1 -U postgres_user -d postgres_tests -c "CREATE EXTENSION IF NOT EXISTS postgis;" env: PGPORT: 5432
3. 在Laravel迁移中主动初始化PostGIS扩展
为彻底规避环境配置问题,可在Laravel迁移文件中添加扩展创建语句,确保迁移执行前扩展已存在:
public function up() { // 先创建PostGIS扩展 \Illuminate\Support\Facades\DB::statement('CREATE EXTENSION IF NOT EXISTS postgis;'); Schema::create('your_table_name', function (Blueprint $table) { $table->id(); // 使用geometry类型字段 $table->geometry('location'); $table->timestamps(); }); }
内容的提问来源于stack exchange,提问作者FryingPan
相关产品推荐
相关产品推荐

