如何在AWS Amazon Linux 2023的EC2实例中安装PostGIS扩展
在Amazon Linux 2023(AL2023)的EC2实例上为PostgreSQL 15安装PostGIS扩展
问题背景
我正在运行Amazon Linux 2023(AL2023)的AWS EC2实例上,尝试为PostgreSQL 15安装PostGIS扩展。之前在Amazon Linux 2上使用的方法在新版本中失效了——因为AL2023不再包含extras组件,原本用来安装扩展的amazon-linux-extras命令已经无法使用,这点已通过AWS官方FAQ确认。
我知道AWS RDS自带PostGIS,但当前需求不需要额外服务器,只希望单个EC2实例同时运行应用和数据库。另外我猜测Redhat/CentOS系列的部分仓库可能支持安装,但还没找到安全可靠的相关信息。
附上yum search post*的输出作为参考:
$ yum search post* Last metadata expiration check: 2:05:28 ago on Fri Jun 9 20:36:37 2023. =========================================== Name & Summary Matched: post* ============================================ postfix.x86_64 : Postfix Mail Transport Agent postfix-cdb.x86_64 : Postfix CDB map support postfix-ldap.x86_64 : Postfix LDAP map support postfix-lmdb.x86_64 : Postfix LDMB map support postfix-mysql.x86_64 : Postfix MySQL map support postfix-pcre.x86_64 : Postfix PCRE map support postfix-perl-scripts.x86_64 : Postfix utilities written in perl postfix-pgsql.x86_64 : Postfix PostgreSQL map support postfix-sqlite.x86_64 : Postfix SQLite map support postgresql15.x86_64 : PostgreSQL client programs postgresql15-private-devel.x86_64 : PostgreSQL development header files for this build of PostgreSQL server postgresql15-server-devel.x86_64 : PostgreSQL development header files and libraries ================================================ Name Matched: post* ================================================= postgresql15-contrib.x86_64 : Extension modules distributed with PostgreSQL postgresql15-docs.x86_64 : Extra documentation for PostgreSQL postgresql15-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL postgresql15-plperl.x86_64 : The Perl procedural language for PostgreSQL postgresql15-plpython3.x86_64 : The Python3 procedural language for PostgreSQL postgresql15-pltcl.x86_64 : The Tcl procedural language for PostgreSQL postgresql15-private-libs.x86_64 : The shared libraries required only for this build of PostgreSQL server postgresql15-server.x86_64 : The programs needed to create and run a PostgreSQL server postgresql15-static.x86_64 : Statically linked PostgreSQL libraries postgresql15-test.x86_64 : The test suite distributed with PostgreSQL postgresql15-test-rpm-macros.noarch : Convenience RPM macros for build-time testing against PostgreSQL server postgresql15-upgrade.x86_64 : Support for upgrading from the previous major release of PostgreSQL postgresql15-upgrade-devel.x86_64 : Support for build of extensions required for upgrade process =============================================== Summary Matched: post* =============================================== collectd-postgresql.x86_64 : PostgreSQL plugin for collectd dovecot-pgsql.x86_64 : Postgres SQL back end for dovecot libpq.x86_64 : PostgreSQL client library perl-Module-Package.noarch : Postmodern Perl Module Packaging perl-autouse.noarch : Postpone load of modules until a function is used psutils.noarch : PostScript utilities python3-dnf-plugin-post-transaction-actions.noarch : Post transaction actions Plugin for DNF t1lib.x86_64 : PostScript Type 1 font rasterizer texlive-esstix.noarch : PostScript versions of the ESSTIX, with macro support texlive-pspicture.noarch : PostScript picture support texlive-pstricks.noarch : PostScript macros for TeX
解决方案
方法1:通过EPEL仓库安装(推荐)
AL2023兼容RHEL的包生态,EPEL仓库提供了适配PostgreSQL 15的PostGIS预编译包,操作步骤如下:
- 安装EPEL仓库配置:
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
- 安装PostGIS 3.x(对应PostgreSQL 15):
sudo dnf install postgis30_15 postgis30_15-client
- 启用并验证扩展:
登录PostgreSQL数据库:
psql -U your_username -d your_database
执行以下SQL启用扩展:
CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology; -- 可选,若需拓扑功能
验证安装成功:
SELECT postgis_version();
方法2:从源码编译安装
如果EPEL包无法满足需求,可选择源码编译:
- 安装编译依赖:
sudo dnf install postgresql15-server-devel gcc make gdal-devel geos-devel proj-devel json-c-devel
- 下载并解压PostGIS源码(以3.3.4版本为例,兼容PostgreSQL 15):
wget https://download.osgeo.org/postgis/source/postgis-3.3.4.tar.gz tar xzf postgis-3.3.4.tar.gz cd postgis-3.3.4
- 配置、编译并安装:
./configure --with-pgconfig=/usr/pgsql-15/bin/pg_config make sudo make install
- 同样进入PostgreSQL命令行执行扩展启用命令即可。
注意事项
- 使用EPEL仓库前,建议先更新系统包:
sudo dnf update,避免依赖冲突。 - 源码编译安装后,后续PostgreSQL或系统版本更新时,可能需要重新编译PostGIS以保证兼容性。
内容的提问来源于stack exchange,提问作者Augusto Sisa
相关产品推荐
相关产品推荐

