PG安装基于CentOS7

YUM换源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yum install -y psmisc traceroute net-tools vim wget
cd /etc/yum.repos.d/ && mv CentOS-Base.repo CentOS-Base.repo.bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
mv Centos-7.repo CentOS-Base.repo

yum clean all
yum makecache
yum update -y
yum upgrade -y

yum install ntp -y # 安装 ntp
ntpdate ntp3.aliyun.com # 同步时间,这里以阿里云3号服务器为例,你可以根据需要选择其他服务器
mv /etc/localtime /etc/localtime.bak --默认EDT时间
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime --切换CST中国上海时间
date

VM CentOS设置固定IP

1
2
3
4
5
6
7
8
9
10
11
vim /etc/sysconfig/network-scripts/ifcfg-ens33

dhcp => static

IPADDR=172.16.249.101
GATEWAY=172.16.249.2
NETMASK=255.255.255.0
DNS1=172.16.249.2
DNS2=223.5.5.5

service network restart

关闭防火墙

1
2
3
4
5
6
7
8
9
10
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
cat /etc/selinux/config

systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service

systemctl stop NetworkManager
systemctl disable NetworkManager

网关查看

1
2
3
4
5
6
7
1.cat /etc/resolv.conf (看DNS) 
2.netstat –r
3.cat /etc/sysconfig/network
4.cat /etc/sysconfig/network-scripts/ifcfg-eth0
5.traceroute 第一行就是自己的网关
6.ip route show
7.route -n

源码安装

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
32
33
34
35
36
37
38
39
40
41
42
43
# root
groupadd postgres
useradd -g postgres -m postgres
passwd postgres

yum install perl-ExtUtils-Embed pam-devel libxml2-devel libxslt-devel openldap-devel python39-devel openssl-devel cmake readline-devel gcc -y
mkdir /usr/local/pg14 -p
mkdir /usr/local/pg14/data -p
chown -R postgres:postgres /usr/local/pg14

# postgres
su - postgres
mkdir downloads
cd downloads

# wget http://file.yangtiancheng.cn/pg/postgresql-14.2.tar.gz - 或者手动移入
wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz --no-check-certificate
tar -zxvf postgresql-14.2.tar.gz
chown -R postgres:postgres postgresql-14.2 && cd postgresql-14.2

# 运行
./configure --prefix=/usr/local/pg14


# 编译
make -j8 world && make install-world
make contrib -j8 && make install # 编译插件

make -j8 && make install

# 设置环境变量
vim ~/.bash_profile

export PATH=/usr/local/pg14/bin:$PATH
export PGHOME=/usr/local/pg14
export PGDATA=$PGHOME/data
export PGUSER=postgres
# export PGPORT=5432

source ~/.bash_profile

# 创建出具库集簇
initdb

Sudoer添加

1
2
3
4
vim /etc/sudoers

# 添加
postgres ALL=(ALL) ALL

初始化数据库

  • 第一种方法:postgresql-15-setup initdb

    1
    2
    3
    采用 systemctl enable postgresql-15 && systemctl start postgresql-15进行启动

    PGDATA位置在/var/lib/pgsql/15/data如果找不到,使用 `find / -name postgresql.conf `来查询PGDATA位置;
  • 第二种方法:initdb -D $PGDATA

    1
    采用 pg_ctl -D $PGDATA start 方式进行启动

配置PG防火墙文件和监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd /usr/local/pg14/data
vim postgresql.conf
# listen_addresses = 'localhost'
listen_addresses = '*'
# port = 5432
port = 15432


vim pg_hba.conf
在host all all 127.0.0.1/32 trust上方添加一行:
host all all 0.0.0.0/0 md5


psql -p 15434
alter user root with password 'XXXXX';

启动数据库服务

1
2
# pg_ctl start/stop/restart/reload
pg_ctl -l logfile start

全局参数配置

1
2
3
4
# postgresql.conf => include* + conf文件夹/文件
select name,setting,unit,short_desc from pg_settings where name like 'work_mem%';
select current_setting('work_mem');