AI 摘要
Windows 安装绿色版 PostgreSQL 17:解压二进制包后设环境变量、初始化数据目录、改 postgresql.conf 和 pghba.conf 允许内网访问,注册为 Windows 服务,并给出创建库、用户及 PG15+ 与旧版授权命令。
一、下载
使用绿色安装包,解压后,注册成 windows 服务.
官网下载地址
二、安装
-
解压放到 D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries 目录
-
设置环境变量
1
| D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql
|
- 初始化
1
| initdb.exe -D "D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql\data" -U postgres -W -E UTF8 --locale=C -A scram-sha-256
|
- 修改默认配置
监听地址和日志 data\postgresql.conf
1 2 3 4 5 6 7 8 9 10 11 12 13
| listen_addresses = '*'
logging_collector = on log_directory = 'log' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_rotation_age = 1d log_timezone = 'Asia/Shanghai' timezone = 'Asia/Shanghai'
shared_buffers = 128MB work_mem = 8MB
|
-
修改配置,数据库层面允许内网网段连接
data\pg_hba.conf,追加到最后
1
| host all all 192.168.10.0/24 scram-sha-256
|
- 启动测试
1
| pg_ctl.exe -D "D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql\data" -l "D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql\data\startup.log" start
|
- 查看状态
1
| pg_ctl.exe -D "D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql\data" status
|
- 连接测试
1
| psql.exe -h 127.0.0.1 -p 5432 -U postgres -d postgres
|
- 停止服务
1
| pg_ctl.exe -D "D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql\data" stop -m fast
|
三、注册成 Windows 服务
- 管理员权限下执行
1
| pg_ctl.exe register -N "postgresql-17" -D "D:\Software\PortableApps\postgresql-17.11-1-windows-x64-binaries\pgsql\data" -S auto -w
|
- 卸载服务
1 2
| net stop postgresql-17 pg_ctl.exe unregister -N "postgresql-17"
|
四、常用命令
- 基本操作
- 创建数据库、创建用户
1 2 3 4 5 6 7 8 9 10 11
| CREATE ROLE myapp_user WITH LOGIN PASSWORD '一个强密码';
ALTER ROLE myapp_user CONNECTION LIMIT 20;
CREATE DATABASE myapp_db OWNER = myapp_user ENCODING = 'UTF8' TEMPLATE = template0;
|
- 授权
1
| GRANT ALL ON SCHEMA public TO myapp_user;
|
1 2
| REVOKE ALL ON SCHEMA public FROM PUBLIC; GRANT ALL ON SCHEMA public TO myapp_user;
|