当前位置:   article > 正文

postgresql编译安装

postgresql编译安装

postgresql编译安装

一、下载源码和依赖包

step 1:源码包下载地址:https://www.postgresql.org/ftp/source/

源码下载

step 2:依赖下载地址:http://www.rpmfind.net/

依赖包下载
在这里插入图片描述

二、编译安装数据库

step 1:创建用户和目录

mkdir /opt/postgresql/data
groupadd pgsql
useradd -g pgsql -G pgsql -d /opt/postgresql pgsql
chown -R pgsql:pgsql /opt/postgresql
passwd pgsql #修改密码
密码:rofine@123
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

step 2:上传安装包到服务器

step 3:安装依赖

rpm -ivh ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm
rpm -Uvh readline-6.2-11.el7.x86_64.rpm
rpm -ivh readline-devel-6.2-11.el7.x86_64.rpm
rpm -ivh mpfr-3.1.1-4.el7.x86_64.rpm 
rpm -ivh libmpc-1.0.1-3.el7.x86_64.rpm
rpm -ivh cpp-4.8.5-44.el7.x86_64.rpm 
rpm -Uvh libgomp-4.8.5-44.el7.x86_64.rpm
rpm -Uvh libgcc-4.8.5-44.el7.x86_64.rpm
rpm -ivh kernel-headers-3.10.0-1160.el7.x86_64.rpm
rpm -Uvh glibc-common-2.17-317.el7.x86_64.rpm glibc-2.17-317.el7.x86_64.rpm
rpm -ivh glibc-headers-2.17-317.el7.x86_64.rpm
rpm -ivh glibc-devel-2.17-317.el7.x86_64.rpm
rpm -ivh gcc-4.8.5-44.el7.x86_64.rpm
rpm -ivh zlib-devel-1.2.7-18.el7.x86_64.rpm
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

step 4:切换pgsql用户,解压文件到指定目录

su pgsql
tar -zxvf postgresql-14.6.tar.gz -C /opt/postgresql
  • 1
  • 2

step 5:编译安装

cd /opt/postgresql/postgresql-14.6
./configure --prefix=/opt/postgresql
make && make install
/opt/postgresql/bin/initdb -D /opt/postgresql/data/
touch /opt/postgresql/logfile 
  • 1
  • 2
  • 3
  • 4
  • 5

step 6:配置环境变量

vim ~/.bash_profile
	
		export PGDATA=/opt/postgresql/data
		export PATH=$PATH:$HOME/bin:/opt/postgresql/bin
		export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/postgresql/lib
		export PGDATABASE=postgres
		export PGPORT=5432
source ~/.bash_profile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

step 7:使用pgsql用户启动postgres

cd /opt/postgresql
./pgsql/bin/pg_ctl -D ./pgsql/pgsql_data/ -l ./pgsql/logfile start
waiting for server to start.... done
server started
#查看pgsql状态
ps -ef | grep postgres | grep -v grep
pgsql    13912     1  0 15:36 pts/2    00:00:00 /home/postgresql/bin/postgres -D ./pgsql/pgsql_data
pgsql    13914 13912  0 15:36 ?        00:00:00 postgres: checkpointer process   
pgsql    13915 13912  0 15:36 ?        00:00:00 postgres: writer process   
pgsql    13916 13912  0 15:36 ?        00:00:00 postgres: wal writer process   
pgsql    13917 13912  0 15:36 ?        00:00:00 postgres: autovacuum launcher process   
pgsql    13918 13912  0 15:36 ?        00:00:00 postgres: stats collector process   
pgsql    13919 13912  0 15:36 ?        00:00:00 postgres: bgworker: logical replication launcher   
-bash-4.2$ psql
psql.bin (14.6)
Type "help" for help.

postgres=# \q    //退出
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

step 8:配置postgresql.conf文件

使用pgsql用户执行
打开postgresql.conf文件,修改以下参数

cd /opt/postgresql/pgsql/pgsql_data
vim postgresql.conf #编辑文件
listen_addresses= '*' #地址为*	port = 5432 #修改端口
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

step 9:配置hosts文件

打开/etc/hosts文件,修改以下参数
vim /etc/hosts #编辑文件
127.0.0.1   postgresql
  • 1
  • 2
  • 3

step 10:修改数据库配置


sed -i -e"s/^#listen_addresses =.*$/listen_addresses = '*'/" $DATADIR/postgresql.conf

sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" $DATADIR/postgresql.conf

sed -i -e"s/^shared_buffers =.*$/shared_buffers = 2GB/" $DATADIR/postgresql.conf

sed -i -e"s/^#effective_cache_size = 128MB.*$/effective_cache_size = 4GB/" $DATADIR/postgresql.conf

sed -i -e"s/^#work_mem = 1MB.*$/work_mem = 128MB/" $DATADIR/postgresql.conf

echo "host all all 0.0.0.0/0 md5" >> $DATADIR/pg_hba.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

step 11:添加系统systemd服务启动

cat >> /etc/systemd/system/postgresql.service <<EOF

[Unit]

Description=PostgreSQL database server

After=network.target

[Service]

Type=forking

User=pgsql

Group=pgsql

#Environment=PGDATA=/opt/postgresql/data

#OOMScoreAdjust=-1000

ExecStart=/opt/postgresql/bin/pg_ctl -D /opt/postgresql/data -l /opt/postgresql/logfile start

ExecStop=/opt/postgresql/bin/pg_ctl stop -D /opt/postgresql/data

ExecReload=/opt/postgresql/bin/pg_ctl reload -D /opt/postgresql/data

TimeoutSec=300

[Install]

WantedBy=multi-user.target

EOF

  • 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

systemctl daemon-reload

systemctl start postgresql.service

systemctl enable postgresql.service

step 12:重启数据库

systemctl restart postgresql.service

EOF

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号