Centos7 安装postgres数据库并设置开机自动启动

本文详细介绍了如何在CentOS7上安装PostgreSQL 9.6.23版本,包括清除旧版本、创建数据目录、设置权限、解压安装包、初始化数据库、修改配置文件以允许远程连接,并设置开机自动启动。通过这些步骤,确保了数据库在服务器重启后仍能正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Centos7 安装postgres数据库并开机自动启动postgres服务

说明:

1, Linux版本:centos7

2,postgres:postgresql-9.6.23.tar.gz 官网选择版本下载即可

3,安装之前清除之前的旧版本

4,位置说明:

/home/postgres/data/ 数据目录

/home/postgres/pgsql/ 解压之后的文件路径

/home/ 安装包路径

安装步骤:

# 添加用户和设置密码
useradd postgres   
passwd postgres

# 创建文件夹
mkdir -p /home/postgres/data/
# 赋予文件权限
chown -R postgres:postgres  /home/postgres/data/
chmod -R 775 /home/postgres/data/

#解压
#我的安装包在 /home/postgres 目录下 
cd /home/postgres 
# 解压到/home/postgres/ 会默认生成pgsql文件夹
tar -xzf postgresql-9.6.23.tar.gz -C /home/postgres/
# 赋予权限
chmod -R 777 pgsql

#进入postgres用户中,注意:中间的中划线 - 
su - postgres  
#进入到bin目录中
cd /home/postgres/pgsql/bin
#初始化 成功之后会有提示
./initdb -D /home/postgres/data/
# Ctrl + D 退出当前

#修改配置文件
cd /home/postgres/data
#分别复制下面三条命令执行修改即可完成修改,也可以手动通过vi/vim命令修改,
# 开启数据库连接工具远程连接;放开5432端口注释,注意服务器防火墙
sed -i 's/127.0.0.1\/32            trust/0.0.0.0\/0               trust/g' pg_hba.conf
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" postgresql.conf
sed -i 's/#port = 5432/port = 5432/g' postgresql.conf

#启动数据库
#切换用户
su - postgres
#进入bin
cd /home/postgres/pgsql/bin
#开启
/home/postgres/pgsql/bin/pg_ctl  -D /home/postgres/data/ -l logfile start
#查看状态
/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/data/ -l logfile status
#停止
/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/data/ -l logfile stop

#如果报错:tbea@linx:/home/postgres/pgsql/bin$ /bin/sh: logfile: 权限不够,则需要赋予文件权限
 chmod 777 logfile 
#如果报错:sh: history: /home/postgres/.bash_history: 无法创建: 权限不够; 是因为 文件夹 “/var/lib/pgsql/9.3/data”权限错误, 权限应该是 rwx (0700)。
# 进入data目录
chmod -R 0700 data

 #启动之后,则可以通过工具访问数据库

这样还没完,如果服务器重启了,postgres服务是启动不了的;需要做开机启动

Postgres开机自动启动:

文件postgresql:

#! /bin/sh

# chkconfig: 2345 98 02
# description: PostgreSQL RDBMS

# This is an example of a start/stop script for SysV-style init, such
# as is used on Linux systems. You should edit some of the variables
# and maybe the 'echo' commands.
#
# Place this file at /etc/init.d/postgresql (or
# /etc/rc.d/init.d/postgresql) and make symlinks to
# /etc/rc.d/rc0.d/K02postgresql
# /etc/rc.d/rc1.d/K02postgresql
# /etc/rc.d/rc2.d/K02postgresql
# /etc/rc.d/rc3.d/S98postgresql
# /etc/rc.d/rc4.d/S98postgresql
# /etc/rc.d/rc5.d/S98postgresql
# Or, if you have chkconfig, simply:
# chkconfig --add postgresql
#
# Proper init scripts on Linux systems normally require setting lock
# and pid files under /var/run as well as reacting to network
# settings, so you should treat this with care.

# Original author: Ryan Kirkpatrick <pgsql@rkirkpat.net>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation prefix
prefix=/home/postgres/pgsql

# Data directory
PGDATA="/home/postgres/data"

# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$PGDATA/serverlog"

# It's often a good idea to protect the postmaster from being killed by the
# OOM killer (which will tend to preferentially kill the postmaster because
# of the way it accounts for shared memory). To do that, uncomment these
# three lines:
#PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
#PG_MASTER_OOM_SCORE_ADJ=-1000
#PG_CHILD_OOM_SCORE_ADJ=0
# Older Linux kernels may not have /proc/self/oom_score_adj, but instead
# /proc/self/oom_adj, which works similarly except for having a different
# range of scores. For such a system, uncomment these three lines instead:
#PG_OOM_ADJUST_FILE=/proc/self/oom_adj
#PG_MASTER_OOM_SCORE_ADJ=-17
#PG_CHILD_OOM_SCORE_ADJ=0

## STOP EDITING HERE

# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# What to use to start up the postmaster. (If you want the script to wait
# until the server has started, you could use "pg_ctl start" here.)
DAEMON="$prefix/bin/postmaster"

# What to use to shut down the postmaster
PGCTL="$prefix/bin/pg_ctl"

set -e

# Only start if we can find the postmaster.
test -x $DAEMON ||
{
 echo "$DAEMON not found"
 if [ "$1" = "stop" ]
 then exit 0
 else exit 5
 fi
}

# If we want to tell child processes to adjust their OOM scores, set up the
# necessary environment variables. Can't just export them through the "su".
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


# Parse command line parameters.
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

exit 0

创建文件postgresql文件;

#在/etc/init.d/linux /etc/init.d/ 下创建文件postgressql
 cd /etc/init.d/linux /etc/init.d/
 
 # 创建文件
touch postgresql

#复制以上文件内容,拷贝到postgresql中
vi  /etc/init.d/linux /etc/init.d/postgresql

# 按键 i ,进入编辑模式  复制内容 粘贴 然后wq!保存;

#给postgresql分配执行权限
chmod a+x /etc/init.d/postgresql

# 添加开机启动
 chkconfig --add postgresql 

#重启服务器验证设置是否成功
 reboot
 
#重启用工具连接测试是否正常启动

#postgresql文件内容说明:
prefix设置为你的安装路径
PGUSER设置为操作postgreSQL的用户(默认为postgres)
PGLOG是日志路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青朽_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值