1、下载地址
2、选择版本
Downloads -> Source -> v14.0,选择tar.gz下载
2、上传至 /data/postgresql/路径并解压
解压,查看目录
[root@VM-8-15-centos postgresql]# tar -zxvf postgresql-14.0.tar.gz
3、编译
[root@VM-8-15-centos postgresql]# cd postgresql-14.0/
[root@VM-8-15-centos postgresql-14.0]# ./configure --prefix=/usr/local/postgresql
4、安装
[root@VM-8-15-centos postgresql-14.0]# make && make install
5、进入安装后的目录,查看目录结构
[root@VM-8-15-centos ~]# cd /usr/local/postgresql
6、创建目录 data、log
[root@VM-8-15-centos postgresql]# mkdir /usr/local/postgresql/data
[root@VM-8-15-centos postgresql]# mkdir /usr/local/postgresql/log
7、加入系统环境变量
[root@VM-8-15-centos ~]# vim /etc/profile
在最后写入如下内容
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_301
export CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
使配置文件生效
[root@VM-8-15-centos ~]# source /etc/profile
8、增加用户 postgres 并赋权
[root@VM-8-15-centos postgresql]# useradd postgres
[root@VM-8-15-centos postgresql]# chown -R postgres:root /usr/local/postgresql
9、初始化数据库
[root@VM-8-15-centos ~]# su postgres
[root@VM-8-15-centos root]$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
注意:
不能在 root 用户下初始数据库,否则会报错
[root@VM-8-15-centos ~]# /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
11、编辑配置文件
[postgres@VM-8-15-centos ~]$ vim /usr/local/postgresql/data/postgresql.conf
[postgres@VM-8-15-centos ~]$ vim /usr/local/postgresql/data/pg_hba.conf
添加一行
host all all 0.0.0.0/0 trust
说明:
TYPE:pg的连接方式,local:本地unix套接字,host:tcp/ip连接
DATABASE:指定数据库
USER:指定数据库用户
ADDRESS:ip地址,可以定义某台主机或某个网段,32代表检查整个ip地址,相当于固定的ip,24代表只检查前三位,最后一 位是0~255之间的任何一个
METHOD:认证方式,常用的有ident,md5,password,trust,reject。
md5是常用的密码认证方式。
password是以明文密码传送给数据库,建议不要在生产环境中使用。
trust是只要知道数据库用户名就能登录,建议不要在生产环境中使用。
reject是拒绝认证。
12、启动服务
[postgres@VM-8-15-centos ~]$ pg_ctl start -l /usr/local/postgresql/log/pg_server.log
13、查看版本
[postgres@VM-8-15-centos ~]$ psql -V
psql (PostgreSQL) 14.0
14、登录数据库
[postgres@VM-8-15-centos ~]$ psql -U postgres -d postgres
psql (14.0)
Type "help" for help.
postgres=#
第三方工具测试连接: