赞
踩
一、安装
1、下载源码包
-
- # wget https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz
-
- # wget --no-check-certificate https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz
-
-
2、编译安装
-
- # yum install -y readline-devel zlib-devel make gcc
-
- # tar xf postgresql-15.3.tar.gz
-
- # cd postgresql-15.3
-
- # ./configure --prefix=/opt/pgsql
-
- # make && make install
-
-
3、初始化数据库
3.1 创建用户
- # groupadd postgres
-
- # 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添加如下环境变量:
- # su - postgres
-
- $ vim .bashrc
-
- export PGPORT=5432
-
- export PGHOME=/opt/pgsql
-
- export PGDATA=/opt/pgsql/data
-
- export PGLOG=/opt/pgsql/log/serverlog
-
- export PATH=${PGHOME}/bin:$PATH
-
- 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,进行如下配置修改:
- # vim /etc/init.d/postgresql |egrep -v "^#|^$"
- # chkconfig: 2345 10 90
- # description: Start and Stop postgres
- prefix=/opt/pgsql
- PGDATA="/opt/pgsql/data"
- PGUSER=postgres
- PGLOG="$PGDATA/serverlog"
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DAEMON="$prefix/bin/postmaster"
- PGCTL="$prefix/bin/pg_ctl"
- set -e
- test -x $DAEMON ||
- {
- echo "$DAEMON not found"
- if [ "$1" = "stop" ]
- then exit 0
- else exit 5
- fi
- }
- if [ -e "$PG_OOM_ADJUST_FILE" -a -n "$PG_CHILD_OOM_SCORE_ADJ" ]
- then
- DAEMON_ENV="PG_OOM_ADJUST_FILE=$PG_OOM_ADJUST_FILE PG_OOM_ADJUST_VALUE=$PG_CHILD_OOM_SCORE_ADJ"
- fi
- case $1 in
- start)
- echo -n "Starting PostgreSQL: "
- test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
- su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
- echo "ok"
- ;;
- stop)
- echo -n "Stopping PostgreSQL: "
- su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
- echo "ok"
- ;;
- restart)
- echo -n "Restarting PostgreSQL: "
- su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
- test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
- su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
- echo "ok"
- ;;
- reload)
- echo -n "Reload PostgreSQL: "
- su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
- echo "ok"
- ;;
- status)
- su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
- ;;
- *)
- # Print help
- echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
- exit 1
- ;;
- esac
-
- # chmod u+x /etc/init.d/postgresql
- # chkconfig --add postgresql
- # service postgresql start
- # chkconfig postgresql on
- # chkconfig --list
5、测试数据库连接
- # su postgres
- # psql
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。