环境介绍:Linux版本centos7、Postgresql版本12.1
安装步骤
1、设置保存安装包的目录
cd /home

2.到官网下载源包
到官网下载postgresql-12.1.tar.gz:https://www.postgresql.org/ftp/source/
下载完成通过FTP上传至home目录下
3.解压源包
tar -zxvf postgresql-12.1.tar.gz
4.进入解压后目录
cd /home/postgresql-12.1

5.下载依赖包
yum install -y bison
yum install -y flex
yum install -y readline-devel
yum install -y zlib-devel
6.配置生成Makefile到安装目录
./configure --prefix=/home/postgresql-12.1
7、编译并安装
make
make install
8.创建添加postgres用户到postgres组
groupadd postgres
useradd -g postgres postgres
chown -R postgres:postgres /home
mkdir -p /home/postgresql-12.1/data
su postgres
9.初始化数据库
/home/postgresql-12.1/bin/initdb -D /home/postgresql-12.1/data/
操作数据库
/home/postgresql-12.1/bin/pg_ctl -D /home/postgresql-12.1/data/ -l logfile start --启动数据库
/home/postgresql-12.1/bin/pg_ctl -D /home/postgresql-12.1/data/ stop --停止数据库
/home/postgresql-12.1/bin/pg_ctl restart -D /home/postgresql-12.1/data/ -m fast --重启数据库
配置步骤
1、修改postgresql.conf
cd /home/postgresql-12.1/data/
vim postgresql.conf
原本Listen_addresses被注释了,放开并指定具体IP

2、修改postgresql.conf
vim pg_hba.conf
新增host all alll 0.0.0.0/0 trust

3、添加环境变量
vim /etc/profile
export PGHOME=/home/postgresql-12.1
export PGDATA=/home/postgresql-12.1/data
PATH=$PATH:$HOME/bin:$PGHOME/bin

4、设置开机自启动
cd /home/postgresql-12.1/contrib/start-scripts
5.授权
chmod a+x linux
6.复制到启动文件中
cp linux /etc/init.d/postgresql (复制linux文件到/etc/init.d目录下,并更名postgresql)
vim linux /etc/init.d/postgresql
7.添加到开机启动
chkconfig --add postgresql (添加开机启动项目)
chkconfig (看下是否设置成功)

8.开启5432端口(防火墙关闭了就不要操作了)
$ firewall-cmd --zone=public --list-ports
$ firewall-cmd --zone=public --add-port=5432/tcp --permanent (添加5432端口)
$ firewall-cmd --reload (重启防火墙)
9.启动服务
service postgresql start
10.设置默认密码
psql -U postgres
postgres=# ALTER USER postgres with encrypted password '123456';
就完成了测试连接即可
