原 【DB宝70】在Windows中绿色安装PostgreSQL13.3数据库
Tags: 原创PGDocker安装部署脚本其它Windows安装
1、安装概述
PG安装方法很多,和MySQL类似,给用户提供很大的选择空间。如:RPM包安装(在线、离线)、源码编译安装、系统自带、二进制、NDB安装等。
https://yum.postgresql.org/rpmchart.php
https://yum.postgresql.org/11/redhat/rhel-6-x86_64/repoview/postgresqldbserver11.group.html
https://www.postgresql.org/ftp/source/
打开 PostgreSQL 官网 https://www.postgresql.org/,点击菜单栏上的 Download ,可以看到这里包含了很多平台的安装包,包括 Linux、Windows、Mac OS等 。
各个安装包:https://www.postgresql.org/ftp/source/
Linux 我们可以看到支持 Ubuntu 和 Red Hat 等各个平台,点击具体的平台链接,即可查看安装方法:
点击上图中的 file browser,我们还能下载 PostgreSQL 最新的源码。
2、下载Windows的二进制格式
二进制格式下载地址:https://www.enterprisedb.com/download-postgresql-binaries
3、解压安装包
下载完成后,把改压缩包解压到D盘的相关目录下,麦老师这里解压到D:\db\PostgreSQL\13.3目录下。
4、配置环境变量
配置环境变量,编辑批处理文件 配置环境变量.vbs(永久生效),然后执行该vbs文件:
1 2 3 4 5 6 7 8 9 10 11 | on error resume next set sysenv=CreateObject("WScript.Shell").Environment("system") '系统环境变量的数组对象 Path = CreateObject("Scripting.FileSystemObject").GetFolder(".").Path '添加变量 sysenv("PGHOME")="D:\db\PostgreSQL\13.3" sysenv("PGHOST")="localhost" sysenv("Path")=sysenv("PGHOME")+"\bin;"+sysenv("Path") sysenv("PGLIB")=sysenv("PGHOME")+"\lib" sysenv("PGDATA")=sysenv("PGHOME")+"\data" wscript.echo "PostgreSQL环境变量安装成功!不需要重新启动计算机!" |
或在cmd中执行如下代码:
1 2 3 4 5 | SETX "PGHOME" "D:\db\PostgreSQL\13.3" SETX "PGHOST" "localhost" SETX "PGLIB" "%PGHOME%\lib" SETX "PGDATA" "%PGHOME%\data" SETX "PATH" "%%PGHOME%%\bin;%path%" |
5、初始化数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | C:\Users\lhrxxt>D:\db\PostgreSQL\13.3\bin\initdb.exe -D "D:\db\PostgreSQL\13.3\data" -E UTF-8 --locale=chs -U postgres -W The files belonging to this database system will be owned by user "lhrxxt". This user must also own the server process. The database cluster will be initialized with locale "Chinese_China.936". initdb: could not find suitable text search configuration for locale "Chinese_China.936" The default text search configuration will be set to "simple". Data page checksums are disabled. Enter new superuser password: Enter it again: creating directory D:/db/PostgreSQL/13.3/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... windows selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Asia/Hong_Kong creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: ^"D^:^\db^\PostgreSQL^\13^.3^\bin^\pg^_ctl^" -D ^"D^:^\db^\PostgreSQL^\13^.3^\data^" -l logfile start |
6、启动数据库
1 | pg_ctl -D D:\db\PostgreSQL\13.3\data -l logfile start |
7、注册系统服务,需管理员权限方可
1 | pg_ctl register -N PostgreSQL -D D:\db\PostgreSQL\13.3\data |
查看已经注册的服务:win+R在弹出的运行框中,输入:services.msc,如下: