1. 程式人生 > >CentOS6.5下tar包安裝postgresql-9.6.1資料庫

CentOS6.5下tar包安裝postgresql-9.6.1資料庫

1.下載安裝包

從Postgres官方網站下載postgresql-9.6.1-1-linux-x64-binaries.tar.gz安裝包

2.建立使用者

建立Postgres使用者:

useradd postgres                                                                                                                                                                         passwd postgres(postgres)

3.解壓

為了保證我們使用postgres使用者安裝完成後其他使用者也能使用,我們採用root使用者解壓安裝包到/usr目錄中,再將相應目錄的許可權改回postgres

su
cd /data/soft/postgres/
tar -zxvf postgresql-9.6.1-1-linux-x64-binaries.tar.gz -C /usr

4.更改目錄許可權

切換到/usr目錄,找到剛才解壓的pgsql資料夾,將pasql資料夾的所有者改回postgres

chown -R postgres:postgres /usr/pgsql

5.建立資料目錄

在/opt目錄下建立postgres的資料目錄,並更改資料夾所有者為postgres

cd /opt 
mkdir postgres
cd postgres
mkdir 9.6
cd 9.6
mkdir data
cd /opt
chown -R postgres:postgres /opt/postgres

如果後期忘記了posgresql安裝到什麼目錄了,可以通過查詢pg_hba.conf,來定位postgresql的位置

切換到postgres使用者下新增PG_HOME和PGDATA環境變數

vim ~/.bash_profile
export PG_HOME=/usr/pgsql
export PGDATA=/opt/postgres/9.6/data
export PATH=$PATH
:$PG_HOME/bin

6.初始化資料庫

切換到postgres使用者,初始化資料庫

find . -name initdb
su - postgres
/usr/pgsql/bin/initdb -D /opt/postgres/9.6/data

執行完成後結果如下:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /opt/postgres/9.6/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/pgsql/bin/pg_ctl -D /opt/postgres/9.6/data -l logfile start

如果想對postgres進行配置,可以編輯如下檔案:

cd /opt/postgres/9.6/data
vim postgresql.conf

7.啟動資料庫

先建立日誌目錄

cd /opt/postgres/9.6/
mkdir logs

啟動資料庫:

/usr/pgsql/bin/pg_ctl -D /opt/postgres/9.6/data -l /opt/postgres/9.6/logs/postgres.log start

檢視服務是否啟動成功

ps -ef | grep postgres

當看到類似下面的結果時,說明服務啟動成功了

[postgres@hadoop1 9.6]$ ps -ef | grep postgres
root       3659   3644  0 09:46 pts/1    00:00:00 su - postgres
postgres   3660   3659  0 09:46 pts/1    00:00:00 -bash
root       4056   3751  0 10:15 pts/1    00:00:00 su - postgres
postgres   4057   4056  0 10:15 pts/1    00:00:00 -bash
postgres   4946      1  0 10:26 pts/1    00:00:00 /usr/pgsql/bin/postgres -D /opt/postgres/9.6/data
postgres   4948   4946  0 10:26 ?        00:00:00 postgres: checkpointer process                   
postgres   4949   4946  0 10:26 ?        00:00:00 postgres: writer process                         
postgres   4950   4946  0 10:26 ?        00:00:00 postgres: wal writer process                     
postgres   4951   4946  0 10:26 ?        00:00:00 postgres: autovacuum launcher process            
postgres   4952   4946  0 10:26 ?        00:00:00 postgres: stats collector process                
postgres   5560   4057  0 10:30 pts/1    00:00:00 ps -ef
postgres   5561   4057  0 10:30 pts/1    00:00:00 grep postgres

呼叫如下命令檢視postgres服務:

service --status-all | grep postgres

然後發現並沒有結果,因此需要將postgresql註冊到服務列表

8.修改postgres使用者的訪問密碼並測試建庫建表

PostgreSQL 資料庫預設會建立一個postgres的資料庫使用者作為資料庫的管理員,預設密碼為空,我們需要修改為指定的密碼,這裡設定為’postgres’
直接在控制檯輸入以下命令:

su - postgres
psql
# ALTER USER postgres WITH PASSWORD 'postgres';
# select * from pg_shadow ;
# create database project;
# \c project

project=# create table person(id integer, name text);
project=# insert into person values (1, 'jimmy');
project=# select * from person

可以看到我們剛才插入的那條資料

9.將postgresql-9.6新增到服務列表

9.1CentOS6作業系統

su root
cd /etc/init.d/
touch postgresql-9.6
vim postgresql-9.6

新增以下內容(該檔案參考postgres解壓目錄中的linux檔案生成):

#! /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 <[email protected]>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation prefix
prefix=/usr/pgsql

# Data directory
PGDATA="/opt/postgres/9.6/data/"

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

# Where to keep a log file
PGLOG="/opt/postgres/9.6/logs/postgres.log"

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

# 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 -m fast"
    echo "ok"
    ;;
  restart)
    echo -n "Restarting PostgreSQL: "
    su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"
    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

需要根據安裝路徑修改配置中相應的路徑資訊
接著就可以使用service postgresql-9.6 start/stop/status來操作postgresql的啟停了

9.2CentOS7作業系統

su root
cd /lib/systemd/system/
vim postgres.service

新增以下內容:

[Unit]
Description=postgres
After=network.target

[Service]
User=postgres
Group=postgres
Type=forking
ExecStart=/usr/pgsql/bin/pg_ctl -D /opt/postgres/9.6/data -l /opt/postgres/9.6/logs/postgres.log start
ExecReload=
ExecStop=/usr/pgsql/bin/pg_ctl -D /opt/postgres/9.6/data stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

10.把postgresql加入自啟動列表

10.1CentOS6作業系統

cd /etc/init.d
chkconfig --add postgresql-9.6

檢視一下自啟動列表

chkconfig --list

在這裡可以看到postgresq-9.6已經在其中了。

10.2CentOS7作業系統

systemctl enable postgres.service

檢視自啟動列表中是否有postgres:

[root@hadoop2 postgres]# systemctl list-units --type=service |grep postgres
postgres.service                                      loaded active running postgres

11.配置postgresql允許遠端訪問

只需要修改data目錄下的pg_hba.conf和postgresql.conf這兩個檔案:
pg_hba.conf:配置對資料庫的訪問許可權;
postgresql.conf:配置PostgreSQL資料庫伺服器的相應的引數

11.1修改pg_hba.conf

vim /opt/postgres/9.6/data/pg_hba.conf

在IPV4中新增下面那一行內容

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0                   md5
#表示允許任意網段的任意機器通過密碼驗證的方式訪問到該資料庫

重新載入postgresql配置檔案(可選):

su - postgres
pg_ctl reload

11.2修改postgresql.conf

vim /opt/postgres/9.6/data/postgresql.conf

定位到listen_addresses,並將localhost改為*

listen_addresses = '*'           # what IP address(es) to listen on;

注:修改完配置後需要重新啟動postgresql遠端連線才能生效
至此,postgresql的安裝和配置已經全部完成