前排提示:刚开始安装的是pgsql15,但是安装好之后连接图形化工具操作数据库时发现只有第一次启动服务器时可以连接,之后均无法连接,查找资料后发现postgresql15版本更新后去掉了一个系统表,导致部分图形化工具会连接失败,因此安装了pgsql14.2,对了安装时注意路径,注意路径,注意路径,重要的事情说三遍,不要安装位置和我的不一样但是直接复制我的路径。
#进入安装目录(自选,我通常安装在opt下)
$ cd /opt
#创建数据库的文件夹
$ mkdir postgresql
#官网下载14.2源码tar包:https://www.postgresql.org/ftp/source/
#通过shell工具发送到linux系统上,或直接用linux系统下载tar包
#移动到安装目录
$ mv postgresql-14.2.tar.gz /opt/postgresql/
#解压
$ tar -zxvf postgresql-14.2.tar.gz
#重命名为pg14,此后完整路径:opt/postgresql/pg14
$ mv postgresql-14.2 pg14
#安装所需要的依赖库及编译工具,已安装跳过
$ yum install -y bison
$ yum install -y flex
$ yum install -y gcc
$ yum install -y readline-devel
$ yum install -y zlib-devel.x86_64
#配置选项生成Makefile:
$ ./configure --prefix=/opt/postgresql/pg14
#开始编译
$ make
$ make install
#postgresql 不能以 root 用户运行,所以创建 postgres 用户:
$ adduser postgres
#给postgres添加sudo权限
$ cd /etc
$ chmod +w sudoers
$ vim sudoers
#在打开的文件中于root用户下一行添加postgres ALL=(ALL) ALL
$ chmod -w sudoers
#创建data目录
$ mkdir /opt/postgresql/pg14/data
#关联组
$ chown postgres /opt/postgresql/pg14/data
#切换到 postgres 用户,带-,连带环境一起切换
$ su - postgres
# 初始化数据库
$ /opt/postgresql/pg14/bin/initdb -D /opt/postgresql/pg14/data
#提示initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/opt/postgresql/pg14/bin/pg_ctl -D /opt/postgresql/pg14/data -l logfile start
#创建logfile
$ touch /opt/postgresql/pg14/data/logfile
#修改配置文件,文件在data目录下
pg_hba.conf ,把其中的127.0.0.1/32 改为0.0.0.0/0(全0即为对所有ip地址开放,生产环境慎用)
postgresql.conf ,listen_address去掉注释并将后面的内容改为“*”
#设置环境变量
$ vim ~/.bashrc
#添加
export PGHOME=/opt/postgresql/pg14
export PGDATA=/opt/postgresql/pg14/data
PATH=$PATH:$HOME/bin:$PGHOME/bin
export LD_LIBRARY_PATH=/opt/postgresql/pg14/lib
#刷新
$ source .bashrc
#启动服务器
$ /opt/postgresql/pg14/bin/pg_ctl -D /opt/postgresql/pg14/data/ -l /opt/postgresql/pg14/data/logfile start
#启动客户端并修改密码,因为服务器已启动,且环境变量也已经设置好,因此可以直接通过psql启动
$ /opt/postgresql/pg14/bin/psql
#postgres=# \password postgres
Enter new password for user "postgres":
Enter it again:
#退出
postgres=# \q
#设置开机启动
$ cd /opt/postgresql/pg14/contrib/start-scripts #此目录在tar包解压出的目录下,如未移动目录无视此注释
#修改权限
$ sudo chmod a+x linux #包括后续开放防火墙端口,均需要root权限,因此可先切换回root用户再进行操作
#复制linux文件到/etc/init.d目录下,并更名postgresql
$ sudo cp linux /etc/init.d/postgresql
#查看配置信息
$ sudo vim /etc/init.d/postgresql
#修改以下两条
prefix=/opt/postgresql/pg14(安装程序的文件路径)
PGDATA=/opt/postgresql/pg14/data (数据存放目录)#如果和自己的路径一致就不需要修改
$ chkconfig --add postgresql (添加开机启动项目)
#如无此命令可通过yum进行安装
$ chkconfig (看下是否设置成功)
#开放默认的5432端口
#centos7自带firewall防火墙。
#1、启动FirewallD服务命令:
$ systemctl start firewalld.service #开启服务
$ systemctl enable firewalld.service #设置开机启动
#2、查看FirewallD防火墙状态:
$ systemctl status firewalld
#开放5432端口
$ firewall-cmd --permanent --add-port=5432/tcp
#查看防火墙的开放的端口
$ firewall-cmd --permanent --list-ports
#重启防火墙
$ firewall-cmd --reload
#启动pgsql服务器
#启动后外部图形化软件便可以连接了,连接时别忘记通过ip add查询ip地址
$ service postgresql start
#查看PostgreSQL服务
$ ps -ef | grep postgres
#启动psql,注意这里启动的是客户端,不是服务器
$ psql
#注意启动psql客户端只能在非root环境下,如之前切换到了root用户需重新切换回postgres用户
#pgsql的每个用户对应同名的linux用户,使用时需要注意