環境:redhat_6.4_x86-64
postgresql版本:postgresql-9.4.1
首先下載pg安裝包:http://www.postgresql.org/download/
1、解壓源碼包:
# tar -vxf postgresql-9.4.1.tar.gz
2、編譯安裝:
# cd postgresql-9.4.1
# ./configure --prefix=/usr/local/pgsql
#make
#make install
這裏需要注意:若要安裝postgresql自帶的相關插件,用下面的命令即可全部安裝,插件存放目錄爲源碼包的contrib目錄內。但若make world 的話,編譯時間較長,且需要考慮依賴性問題。
#make world
#make install
3、創建用戶與用戶組,postgresql需要用普通用戶啓動,若實在想用root啓動,則需要修改pgsql的源碼,去除對root的的檢測才行:
#groupadd pgsql
#useradd pgsql -d /pgsql -s /bin/bash -g pgsql -p pgsql
4、配置pgsql的環境變量
#su pgsql
$cd
$vi .bash_profile
在配置文件中添加如下環境變量並保存退出vi
export PGDATA=/usr/local/pgsql/data
export PGHOME=/usr/local/pgsql
export PGLIB=/usr/local/pgsql/lib
$source .bash_profile
$echo $PGDATA
/usr/local/pgsql/data
5、創建data目錄
$su root
password:
#cd /usr/local/pgsql
#mkdir data
6、修改data目錄所有者與權限
#chown -R pgsql.pgsql data
#chomd -R 0700 data
#ll
drwxr-xr-x. 2 root root 4096 5月 7 19:42 bin
drwx------. 18 pgsql pgsql 4096 5月 7 22:02 data
drwxr-xr-x. 6 root root 4096 5月 7 19:42 include
drwxr-xr-x. 4 root root 4096 5月 7 19:42 lib
drwxr-xr-x. 6 root root 4096 5月 7 19:42 share
7、初始化數據庫
#su pgsql
#/usr/local/pgsql/bin/initdb -D $PGDATA --locale=C -U postgres -W -E utf8
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/pgsql/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
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
Enter new superuser password:
Enter it again:
setting password ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... 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/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
8、修改postgresql配置文件,
$vi /usr/local/pgsql/data/pg_hba.conf
#允許192.168網段的所有ip使用postgres用戶登錄所有數據庫,驗證密碼。
host all postgres 192.168.0.0/16 md5
$vi /usr/local/pgsql/data/postgresql.conf
listen_addresses = '*'
port = 5432
9、配置隨系統啓動,將源碼包中的配置文件拷貝到/etc/rc.d/init.d
$su root
#cp /install/postgresql-9.4.1/contrib/start-scripts/linux /etc/rc.d/init.d/postgresql
#vi /etc/rc.d/init.d/postgresql
#主要配置登錄用戶
## EDIT FROM HERE
# 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"
#chkconfig --add postgresql
#chkconfig --list postgresql
postgresql 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
# chmod +x /etc/rc.d/init.d/postgresql
#service postgresql start
Starting PostgreSQL: ok
#ps -ef | grep post
pgsql 6641 1 0 22:55 ? 00:00:00 /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data
pgsql 6643 6641 0 22:55 ? 00:00:00 postgres: checkpointer process
pgsql 6644 6641 0 22:55 ? 00:00:00 postgres: writer process
pgsql 6645 6641 0 22:55 ? 00:00:00 postgres: wal writer process
pgsql 6646 6641 0 22:55 ? 00:00:00 postgres: autovacuum launcher process
pgsql 6647 6641 0 22:55 ? 00:00:00 postgres: stats collector process
# ln -s /usr/local/pgsql/bin/psql /usr/bin/psql
#su pgsql
$psql -U postgres
Password for user postgres: #初始化數據庫時填的密碼
psql (9.4.1)
Type "help" for help.
postgres=#
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=#
備註:單獨安裝postgresql相關插件
一些輕量級的插件如pgbench等可以用如下方法安裝
進入postgresql的源碼包目錄即安裝包目錄,
將插件拷貝到contrib目錄下,進入插件的目錄,make USE_PGXS=1 make install