Ubuntu 24.04.2 安装 PostgreSQL 16 、PostGIS 3 安装 PostgreSQL 16apt install postgresql-16passwd postgres修改 postgres 用户密码su - postgres注意不要少 -/usr/lib/postgresql/16/bin/initdb -D /var/lib/postgresql/16/main初始化数据目录初始化后需要告诉 PostgreSQL 服务使用这个新位置。编辑主配置文件 /etc/postgresql/16/main/postgresql.conf修改 data_directory 参数data_directory 数据目录 # /var/lib/postgresql/16/main/usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/main -l /var/log/postgresql/postgresql-16-main.log start启动数据库服务-D指定数据目录-l指定日志文件路径/usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/main stop -m fast停止服务-m fast表示快速停止模式会回滚未提交的事务并强制断开客户端连接是最常用的安全停止方式。其他模式还有smart等待客户端主动断开和immediate立即强制退出下次启动需恢复/usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/main restart -m fast重启服务支持 systemd 的环境可用systemctl restart postgresql重启startstoppsql -U postgres, 以 postgres 的身份登录数据库alter user postgres with password abc123;\q 退出/etc/postgresql/16/main/postgresql.conf 可修改#listen_addresses *可修改端口 25432/etc/postgresql/16/main/pg_hba.conf 修改连接# IPv4 local connections: host all all 127.0.0.1/32 trust host all all 0.0.0.0/0 scram-sha-256su root,lsof -i:25432可以查看是否运行systemctl disable postgresql可禁止自启动apt install ufw,ufw enable,ufw allow 25432/tcp安装 PostGis 3apt install postgresql-16-postgis-3su - postgrespsql -U postgrescreate database test_data;\c test_data 连接create extension postgis;select PostGIS_version();验证安装\c postgresselect * from pg_database;drop database test_data;