linux环境配置之Postgresql安装

先贴一个postgresql历史版本地址:https://www.postgresql.org/ftp/source/

本文9.5,可以尝试更高版本安装

1、进入你的安装包存放路径,

cd /data/

wget [https://ftp.postgresql.org/pub/source/v9.5.19/postgresql-9.5.19.tar.bz2](https://ftp.postgresql.org/pub/source/v9.5.19/postgresql-9.5.19.tar.bz2)

2、解压:安装包路径下操作

tar -xvf /data/postgresql-9.5.19.tar.bz2

3、新建安装地址并指向安装路径

mkdir /data/postgresql-9.5/data

mkdir /data/postgresql-9.5/logs

./configure --prefix=/data/postgresql-9.5/postgresql

4、编译并安装(注意所需开发包是否已存在)

#yum install gcc  安装所需编译器

#yum install readline-devel  安装readline-devel  开发包

#yum install zlib-devel  安装 zlib-devel  开发包

make 

make install

5、配置服务启动命令

cd /etc/rc.d/init.d

vi postgresql

#将以下内容保存到文件

#! /bin/sh
prefix=/data/postgresql-9.5 #pg9.5

# Data directory
PGDATA=/data/postgresql-9.5/data

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

# Where to keep a log file
#PGLOG="$PGDATA/serverlog"
PGLOG=/data/postgresql-9.5/logs

## 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

6、建立用户组、用户 授权

groupadd postgres

useradd -g postgres postgres

chown -R postgres:postgres /data/postgresql-9.5/

7、切换用户,进行

su - postgres

cd ~

vi ./.bash_profile

export PGHOME=/data/postgresql-9.5

export PGDATA=/data/postgresql-9.5/data

export PATH=$PATH:$HOME/bin:$PGHOME/bin


source  ./.bash_profile

psql -V  # 查看安装版本  设置成功

8、初始化数据库

initdb -D $PGDATA

#启动数据库 

pg_ctl start -D $PGDATA

#报类似  libpq.so.5  找不到的执行以下
find / -name libpq.so.5
ln -s /data/pgsql/dbhome/lib/libpq.so.5 /usr/lib64/libpq.so.5
pg_ctl start -D $PGDATA</pre>

#添加用户和密码
psql -U postgres
\password
#这是输入你的密码
#再次输入密码
\q  #退出

9、数据库设置

(1)密码访问

cd /data/postgresql-9.5/data

vi pg_hba.conf

#添加如下  保存

host all all 0.0.0.0/0 password

(2)外网访问

vi postgresql.conf`

#添加以下内容(或者找到相关参数修改)

listen_addresses = '*'      
port = 5432

(3)开放端口

firewall-cmd --zone=public --add-port=6378/tcp --permanent

firewall-cmd --reload
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容