- 上传文件到linux环境
root用户登录 ,上传postgresql-11.7.tar.gz
- 解压文件
输入:tar –zxvf postgresql-11.7.tar.gz
- 检查安装环境(root)
1.切换到解压文件的目录下
[root@localhost ~]# cd postgresql-11.7/
2.执行检查环境命令
[root@localhost postgresql-11.7]# ./configure --prefix=/usr/local/pgsql
检查完成后,可能会有如上的提示,这是因为缺少c语言环境,需要安装gcc
外网环境可直接安装 yum install gcc
2.1内网环境可通过配置本地源后进行安装,操作如下
2.1.1创建文件挂载目录
[root@localhost postgresql-11.7]# mkdir /mnt/cdrom
2.1.2挂载光盘到此路径
[root@localhost postgresql-11.7]# mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom
2.1.3设置自动挂载
[root@localhost postgresql-11.7]# vim /etc/fstab
/dev/cdrom /mnt/cdrom iso9660 defaults 0 0
2.1.4配置本地安装源,切换到/etc/yum.repos.d/目录
[root@localhost postgresql-11.7]# cd /etc/yum.repos.d/
2.1.5备份原有的文件
[root@localhost yum.repos.d]# rename .repo .repo.bak *
[root@localhost yum.repos.d]# ls
2.1.6复制原有的备份文件进行修改
[root@localhost yum.repos.d]# cp CentOS-Base.repo.bak CentOS-Base.repo
[root@localhost yum.repos.d]# vim CentOS-Base.repo
[base]
name=CentOS-Local Source
baseurl=file:///mnt/cdrom/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
2.1.7清除yum 源
[root@localhost yum.repos.d]# yum clean all
2.2安装gcc
[root@localhost ~]# yum install gcc
3.安装完成后,再执行第1点和第2点进行安装检查,此时可能会提示
执行命令
[root@localhost postgresql-11.7]# yum install readline-devel
再次进行环境检查(第2点的命令),此时可能会提示
然后,执行命令
[root@localhost postgresql-11.7]# yum install zlib-devel
4.安装成后,再执行第1点和第2点进行安装检查,出现如下界面,表示检查通过
- 开始编译和安装(root)
需要定位到安装文件的目录下,执行如下命令
[root@localhost postgresql-11.7]# make && make install
出现如下界面,表示安装完成
切换到 二.2 检查时所设置的路径,可以发现已经有了pgsql目录
- 创建用户(root)
创建用户组
[root@localhost local]# groupadd postgre
创建用户并分配组
[root@localhost local]# useradd -g postgre postgre
给用户设置密码
[root@localhost local]# passwd postgre
- 创建数据目录和日志目录,并赋权限
创建数据目录
[root@localhost local]# mkdir -p /usr/local/pgsql/data
创建日志目录
[root@localhost local]# mkdir -p /usr/local/pgsql/log
将pgsql目录下的所有文件的权限赋给 postgre
[root@localhost local]# chown -R postgre:postgre /usr/local/pgsql
- 配置环境变量(postgre用户)
切换用户
[root@localhost ~]# su postgre
定位到postgre用户的根目录
[postgre@localhost root]$ cd ~
配置环境变量
[postgre@localhost ~]$ vim .bash_profile
export PGHOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
export PGLOG=/usr/local/pgsql/log/logfile
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
使配置的环境变量生效
[postgre@localhost ~]$ source .bash_profile
- 初始化数据库(postgre用户)
执行如下命令
[postgre@localhost ~]$ initdb -D $PGDATA
出现如下界面,表示初始化成功
- 设置参数文件(postgre用户)
配置监听、端口、最大连接数
[postgre@localhost ~]$ vi /usr/local/pgsql/data/postgresql.conf
Listen_addresses = ‘*’
Port = 5432
Max_connections = 100
配置允许远程主机连接
[postgre@localhost ~]$ vi /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 password
注:最后一列模式的设置,trust表示信任,无需输入密码即可登录;password表示需要输入密码进行登录;md5表示对应输入的密码进行加密并登录
- 启动pg数据库服务(postgre用户)
执行如下命令
[postgre@localhost ~]$ pg_ctl start -D $PGDATA -l $PGLOG
出现如下server started表示启动成功
注:$PGDATA、$PGLOG均是环境变量中配置的路径
- 关闭pg数据库服务(postgre用户)
执行如下命令
[postgre@localhost ~]$ pg_ctl stop -D $PGDATA -m fast
出现如下server stop 表示关闭成功
- 配置系统服务,开机自启动(root用户)
切换到root用户
[postgre@localhost ~]$ su root
进入到root用户下postgresql-11.7解压缩后的文件目录下
[root@localhost postgre]# cd /root/postgresql-11.7/
执行如下命令
[root@localhost postgresql-11.7]# cp contrib/start-scripts/linux /etc/init.d/postgresql
[root@localhost postgresql-11.7]# vim /etc/init.d/postgresql
Prefix=/usr/local/pgsql
PGDATA=”/usr/local/pgsql/data”
PGUSER=postgre
PGLOG=”$prefix/log/logfile”
注:PGUSER=postgres 指的是第五点创建的用户,此时需要修改为 postgre
给postgresql文件赋予执行的权限
[root@localhost postgresql-11.7]# chmod u+x /etc/init.d/postgresql
[root@localhost postgresql-11.7]# chkconfig --add /etc/init.d/postgresql
然后使用root用户进行测试启动文件是否生效
[root@localhost postgresql-11.7]# service postgresql start
出现如下界面,表示服务启动成功
[root@localhost ~]# service postgresql stop
出现如下界面,表示服务停止成功
- 使用root用户执行service postgresql start 启动服务成功后,测试数据库的连接(postgre用户)
在服务启动成功后,切换到postgre用户,执行plsql
[postgre@localhost ~]$ psql
此时,可能会提示psql: FATAL: database "postgre" does not exist;这时需要如下登录,创建数据库用户
[postgre@localhost ~]$ psql template1
template1=# create user postgres with password 'postgres'; 创建用于远程连接的用户
template1=# \q 退出
进行远程连接前,需要关闭防火墙,使用root用户执行如下命令
[root@localhost ~]# systemctl disable firewalld
关闭防火墙后,系统需要重新启动
重启后,使用root用户执行第十点的启动服务
最后,使用Navicat 测试远程登录