一、安装脚本
#!/bin/bash
# author:coralzd powered by MichealDming
# written by coralzd 2022.10.11
# version 1.2.0 build 20221011
#本文件是postgresql-11.6的自动安装脚本,安装前请仔细阅读安装内容
echo "*****注意:此程序将自动安装至postgresql-11.6到/usr/local/目录下,数据库文件将保存至/pgdata/目录下,确认继续?"
read
echo "*****执行安装前请确定此安装程序与安装包(postgresql-11.6.tar.gz)在/root目录,且当前用户为root,确认继续?"
read
#解压安装文件
tar zvxf postgresql-11.6.tar.gz -C /usr/local/src/
echo " 信息:文件解压完毕!"
#安装postgresql-11.6需要的程序包
echo " *****开始安装必要的程序包!"
dnf install -y gcc cpp glibc glibc-devel gcc-c++ make readline readline-devel zlib zlib-devel
echo " 信息:依赖包安装完毕!"
#判断目录是否存在
if [ ! -d "/pgdata" ]; then
mkdir /pgdata
fi
#开始进行源码编译安装
echo " *****开始对postgresql-11.6进行源码编译安装!如程序检查发现仍有依赖包没有安装,请根据错误信息自行安装,然后重试。"
cd /usr/local/src/postgresql-11.6
./configure
echo " 信息:系统检测完毕!Ready to install."
make
make install
echo " 信息:postgresql-11.6程序安装完毕!"
#修改root用户环境变量
echo " *****修改root用户环境变量为:PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin,原环境变量备份为.bash_profile_back"
cd ~
cp ./.bash_profile ./.bash_profile_back
sed -i 's/PATH=\$PATH:\$HOME\/bin/PATH=\$PATH:\$HOME\/bin:\/usr\/local\/pgsql\/bin/g' .bash_profile
echo " 信息:root用户环境变量修改完毕!"
source /root/.bash_profile
echo " 信息:root用户环境变量已生效!"
#添加数据库用户和组
echo " *****添加postgres用户组"
groupadd postgres
echo " *****添加postgres用户,并添加到postgres用户组"
useradd -g postgres postgres
echo " *****设置postgres用户的密码"
passwd postgres
chown -R postgres.postgres /usr/local/pgsql
chown -R postgres.postgres /pgdata
chmod 700 -R /pgdata
echo "数据库开始初始化,请新打开一个终端,在新的终端中1.执行:su - postgres \\切换到postgres用户 2.执行:/usr/local/pgsql/bin/initdb -D /pgdata 完成后回车继续"
read
echo "*****请确定以上初始化操作已经完成,确认继续?"
read
#设置开机启动及系统服务
cp /usr/local/src/postgresql-11.6/contrib/start-scripts/linux /etc/init.d/postgresql
#修改数据库启动变量pgdata
cd /etc/init.d/
sed -i 's/PGDATA=\"\/usr\/local\/pgsql\/data\"/PGDATA=\"\/pgdata\"/g' /etc/init.d/postgresql
chmod +x /etc/init.d/postgresql
chkconfig --add postgresql
chkconfig postgresql on
#创建数据库历史记录文件
touch /usr/local/pgsql/.psql_history
chown postgres:postgres /usr/local/pgsql/.psql_history
#配置监听地址及监听端口
sed -i "59a\listen_addresses = '*'" /pgdata/postgresql.conf
sed -i '64a\port = 5432' /pgdata/postgresql.conf
sed -i 's/PATH=\$PATH:\$HOME\/.local\/bin:\$HOME\/bin/PATH=\$PATH:\$HOME\/.local\/bin:\$HOME\/bin:\/usr\/local\/pgsql\/bin/g' /home/postgres/.bash_profile
source /home/postgres/.bash_profile
echo "信息:数据库监听地址为 * ,监听端口为5432"
:
if service start postgresql; then
ps -ef|grep postgres
echo "*****添加启动启动完成,添加系统服务完成"
fi
echo "*****添加启动启动完成,添加系统服务完成"
二、启动postgresql数据库,连接数据库,设置postgres用密码,修改pg_hba.conf 让密码登录生效
#启动数据库
service postgresql start
#切换到postgres用户
su postgres
#回到postgres用户用户家目录
$ cd
#连接数据库
$ psql
psql (11.6)
Type "help" for help.
postgres=#
如果提示没有psql命令
请编辑/home/postgres/.bash_profile配置文件,添加如下内容,保存后退出。
PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin
export PATH
加载配置文件
$ source /home/postgres/.bash_profile
设置postgres用密码
#连接数据库
[postgres@anolis8 ~]$ psql
psql (11.6)
Type "help" for help.
postgres=# alter user postgres with password 'pass123';
ALTER ROLE
postgres=# show password_encryption;
password_encryption
---------------------
md5
(1 row)
postgres=# exit
修改pg_hba.conf ,重启数据库,让密码登录生效
[postgres@anolis8 ~]$ cd /pgdata/
[postgres@anolis8 pgdata]$ ll
总用量 60
drwx------ 5 postgres postgres 41 10月 13 14:20 base
drwx------ 2 postgres postgres 4096 10月 13 15:45 global
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_commit_ts
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_dynshmem
-rw------- 1 postgres postgres 4513 10月 13 14:20 pg_hba.conf
-rw------- 1 postgres postgres 1636 10月 13 14:20 pg_ident.conf
drwx------ 4 postgres postgres 68 10月 13 16:00 pg_logical
drwx------ 4 postgres postgres 36 10月 13 14:20 pg_multixact
drwx------ 2 postgres postgres 18 10月 13 14:25 pg_notify
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_replslot
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_serial
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_snapshots
drwx------ 2 postgres postgres 6 10月 13 14:25 pg_stat
drwx------ 2 postgres postgres 63 10月 13 16:02 pg_stat_tmp
drwx------ 2 postgres postgres 18 10月 13 14:20 pg_subtrans
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_tblspc
drwx------ 2 postgres postgres 6 10月 13 14:20 pg_twophase
-rw------- 1 postgres postgres 3 10月 13 14:20 PG_VERSION
drwx------ 3 postgres postgres 60 10月 13 14:20 pg_wal
drwx------ 2 postgres postgres 18 10月 13 14:20 pg_xact
-rw------- 1 postgres postgres 88 10月 13 14:20 postgresql.auto.conf
-rw------- 1 postgres postgres 24044 10月 13 14:20 postgresql.conf
-rw------- 1 postgres postgres 45 10月 13 14:25 postmaster.opts
-rw------- 1 postgres postgres 65 10月 13 14:25 postmaster.pid
-rw-rw-r-- 1 postgres postgres 1310 10月 13 14:25 serverlog
[postgres@anolis8 pgdata]$ vim pg_hba.conf
将trust 替换为md5,保存退出,然后重启数据库。
修改前:
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
修改后
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all md5
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
重启数据库,测试密码
[root@anolis8 ~]# service postgresql stop
Stopping PostgreSQL: ok
[root@anolis8 ~]# service postgresql start
Starting PostgreSQL: ok
[root@anolis8 ~]# su postgres
[postgres@anolis8 root]$ cd
[postgres@anolis8 ~]$ psql
Password for user postgres:
psql (11.6)
Type "help" for help.
postgres=#
网友评论