centos7安装postgresql12版本数据库
环境与资源包:
- 系统:CentOS Linux release 7.8.2003 (Core)
- 数据库版本:postgresql12.2
- PostgreSQL官网下载地址:https://www.postgresql.org/ftp/source/v12.2/
编译安装pgsql:
1、从pgsql官网下载postgresql-12.2.tar.gz编译包(小编放在root目录下)
yum install wget
wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
2、安装pgsql所需的依赖包
yum -y install gcc zlib-devel readline-devel readline gcc-c++ make automake
3、开始编译安装
tar -zxvf postgresql-12.2.tar.gz #解压编译包
cd postgresql-12.2
./configure #是检查当前环境能否安装pgsql
make && make install
make clean && make
4、创建postgres用户,用于管理pgsql数据库
adduser postgres
5、初始化数据库,同时启动数据库
su postgres #进入postgres用户
/usr/local/pgsql/bin/initdb -D /home/postgres/data #初始化数据库,指向/home/postgres/data下,方便运维
#启动数据库,将报错日志存入logfile文件内
/usr/local/pgsql/bin/pg_ctl -D /home/postgres/data -l /home/postgres/logfile restart
6、创建用户名和数据库名,数据库还原讲解
cd /usr/local/pgsql/bin/ #找到对应的pgsql安装目录
./createuser -P chongniya66 #创建数据库用户
设置密码:chongniya #设置密码时,是看不到的,细心输入即可
./createdb -O chongniya66 chongni #创建chongni数据库
cd /home/postgres/ #进入postgres用户根目录下
#用于数据库还原,若第一次搭建,暂无pgsql备份,可不执行
/usr/local/pgsql/bin/psql -U chongniya66 chongni<postgres.backup
#暂时退出postgres用户,返回root用户
exit
PS:pgsql自动定时备份:https://blog.csdn.net/weipaiyizhan/article/details/118789038?spm=1001.2014.3001.5501
7、pgsql配置文件修改
cd /home/postgres/data
#postgresql.conf文件配置
vim postgresql.conf
修改内容如下:
listen_addresses = 'localhost' -> listen_addresses = '*'
#pg_hba.conf文件配置,用于远程访问
vim pg_hba.conf
修改内容如下:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
8、postgresql重新载入配置文件(在root用户下执行即可)
cd /home/postgres/
su postgres -c "/usr/local/pgsql/bin/pg_ctl reload -D /home/postgres/data"
重新载入配置文件成功:
9、测试本地数据库工具,是否可以链接pgsql数据库
小编这里习惯性用的是[Navicat Premium 15数据库管理工具]
10、服务器进入本地postgresql数据库
ln -s /usr/local/pgsql/bin/psql /usr/bin/psql #创建快捷指令
su postgres #进入postgres用户
cd ~ #进入postgres用户根目录
psql #打开数据库
至此,centos7.8编译安装postgres-12.2版本完成