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