PostgreSQL安装和简单配置
PostgreSQL安装与使用
目录
- 依赖包的安装
- 源码编译和安装
- 初始化数据库集簇
- 简单配置
依赖包安装
PostgreSQL源码安装依赖以下四个软件包
readline
zlib
flex
bison
在Ubuntu中可是应用以下命令直接进行安装:
sudo apt-get install libreadline6 libreadline6-dev zlib1g-dev flex bison
如果实在redhat系发行版的Linux中可以使用以下命令:
yum install flex bison zlib-devel readline-devel
源码编译和安装
例如:下载的源码包为:
-rwxrw-rw- 1 bingo bingo 19260568 Dec 2 00:27 postgresql-9.6.1.tar.bz2*
tar -jxvf postgresql-9.6.1.tar.bz2 //解压源码包
进入到源码文件夹根目录下执行以下命令:(各项参数可以根据 sudo ./configure --help
获取帮助)
sudo ./configure --prefix=/opt/pgsql --enable-debug CFLAGS=-O0
常用的几个参数如下
sudo make //或者 make world 编译一切可以编译的东西,包括文档(HTML和手册)以及额外的模块(contrib)
sudo make install
mkdir /usr/local/pgsql/data
useradd -s /bin/bash -m -p 123456 postgres
chown -R postgres:postgres /usr/local/pgsql/data
初始化数据库集簇
su postgres
cd /usr/local/pgsql/bin
./initdb -D ../data
./pg_ctl -D ../data start
登录数据库
./psql -h IP -p 5432 -U postgres test
清理
make uninstall // 卸载已安装的文件,不会删除掉任何创建出来的目录
make clean //清理编译过程中生成的文件,并释放磁盘空间
make distclean // 将源码树恢复成发布的状态
自启动配置
PG自启动脚本postgresql复制到/etc/init.d
并在各个启动级别文件夹中创建软连接。如:
ln -s /etc/init.d/postgresql /etc/rc0.d/K02postgresql
#! /bin/sh
# Installation prefix
prefix=/usr/local/pgsql
# Data directory
PGDATA="/usr/local/pgsql/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). Setting the OOM_ADJ value to
# -17 will disable OOM kill altogether. If you enable this, you probably want
# to compile PostgreSQL with "-DLINUX_OOM_ADJ=0", so that individual backends
# can still be killed by the OOM killer.
#OOM_ADJ=-17
## STOP EDITING HERE
# The path that is to be used for the script
PATH=/usr/local/pgsql/bin:/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 -w" here.
# But without -w, pg_ctl adds no value.)
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
}
# Parse command line parameters.
case $1 in
start)
echo -n "Starting PostgreSQL: "
test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
echo "ok"
;;
stop)
echo -n "Stopping PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"
echo "ok"
;;
restart)
echo -n "Restarting PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"
test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
su - $PGUSER -c "$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