当前位置:   article > 正文

CentOS7安装postgresql15

centos7安装postgresql15

一、安装

1、下载源码包

  1. # wget https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz
  2. # wget --no-check-certificate https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz

2、编译安装

  1. # yum install -y readline-devel zlib-devel make gcc
  2. # tar xf postgresql-15.3.tar.gz
  3. # cd postgresql-15.3
  4. # ./configure --prefix=/opt/pgsql
  5. # make && make install

3、初始化数据库

3.1 创建用户

  1. # groupadd postgres
  2. # useradd -g postgres postgres

3.2 创建数据目录(data)和日志目录(log

# mkdir -p /opt/pgsql/{data,log}

3.3 接下来设置权限,将pg的数据目录全部赋给postgres用户,执行以下命令:

# chown -R postgres:postgres /opt/pgsql

3.4 配置环境变量(postgres)

切换到postgres用户,使用命令vi .bashrc添加如下环境变量:

  1. # su - postgres
  2. $ vim .bashrc
  3. export PGPORT=5432
  4. export PGHOME=/opt/pgsql
  5. export PGDATA=/opt/pgsql/data
  6. export PGLOG=/opt/pgsql/log/serverlog
  7. export PATH=${PGHOME}/bin:$PATH
  8. export LD_LIBRARY_PATH=${PGHOME}/lib:$LD_LIBRARY_PATH

3.5 初始化数据库

使用postgres用户执行如下命令:

$ initdb -D $PGDATA -U postgres --locale=en_US.UTF8 -E UTF8

3.6 设置参数文件(postgres)

使用postgres用户,执行 vim /opt/pgsql/data/postgresql.conf 修改如下参数(监听地址、监听端口、最大连接数):

$ vim /opt/pgsql/data/postgresql.conf

 

执行vim /opt/pgsql/data/pg_hba.conf修改如下参数(客户端认证):

$ vim /opt/pgsql/data/pg_hba.conf

4、配置系统服务,开机自启动(root) 

进入postgresql源码包的解压目录(本文为/opt/postgresql-15.3)

执行命令 cp /opt/postgresql-15.3/contrib/start-scripts/linux /etc/init.d/postgresql

然后 vim /etc/init.d/postgresql,进行如下配置修改:

  1. # vim /etc/init.d/postgresql |egrep -v "^#|^$"
  2. # chkconfig: 2345 10 90
  3. # description: Start and Stop postgres
  4. prefix=/opt/pgsql
  5. PGDATA="/opt/pgsql/data"
  6. PGUSER=postgres
  7. PGLOG="$PGDATA/serverlog"
  8. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  9. DAEMON="$prefix/bin/postmaster"
  10. PGCTL="$prefix/bin/pg_ctl"
  11. set -e
  12. test -x $DAEMON ||
  13. {
  14. echo "$DAEMON not found"
  15. if [ "$1" = "stop" ]
  16. then exit 0
  17. else exit 5
  18. fi
  19. }
  20. if [ -e "$PG_OOM_ADJUST_FILE" -a -n "$PG_CHILD_OOM_SCORE_ADJ" ]
  21. then
  22. DAEMON_ENV="PG_OOM_ADJUST_FILE=$PG_OOM_ADJUST_FILE PG_OOM_ADJUST_VALUE=$PG_CHILD_OOM_SCORE_ADJ"
  23. fi
  24. case $1 in
  25. start)
  26. echo -n "Starting PostgreSQL: "
  27. test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
  28. su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
  29. echo "ok"
  30. ;;
  31. stop)
  32. echo -n "Stopping PostgreSQL: "
  33. su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
  34. echo "ok"
  35. ;;
  36. restart)
  37. echo -n "Restarting PostgreSQL: "
  38. su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
  39. test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
  40. su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
  41. echo "ok"
  42. ;;
  43. reload)
  44. echo -n "Reload PostgreSQL: "
  45. su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
  46. echo "ok"
  47. ;;
  48. status)
  49. su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
  50. ;;
  51. *)
  52. # Print help
  53. echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
  54. exit 1
  55. ;;
  56. esac
  57. # chmod u+x /etc/init.d/postgresql
  58. # chkconfig --add postgresql
  59. # service postgresql start
  60. # chkconfig postgresql on
  61. # chkconfig --list

5、测试数据库连接

  1. # su postgres
  2. # psql

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/699876
推荐阅读
相关标签
  

闽ICP备14008679号