[root@test02 init.d]# ll /etc/init.d/postgresql-9.5php
-rwxr-xr-x. 1 root root 10072 May 15 06:34 /etc/init.d/postgresql-9.5linux
1. 檢查PostgreSQL 是否已經安裝sql
# rpm -qa|grep postgres數據庫
若已經安裝,則使用rpm -e 命令卸載vim
[root@test02 bin]# rpm -qa|grep postgresbash
postgresql-8.4.18-1.el6_4.x86_64網絡
postgresql-devel-8.4.18-1.el6_4.x86_64app
postgresql-libs-8.4.18-1.el6_4.x86_64ide
[root@test02 bin]# rpm -e postgresql-devel-8.4.18-1.el6_4.x86_64工具
[root@test02 bin]# rpm -e postgresql-8.4.18-1.el6_4.x86_64
[root@test02 bin]# rpm -e postgresql-libs-8.4.18-1.el6_4.x86_64
[root@test02 soft_bak]# wget http://yum.postgresql.org/9.5/redhat/rhel-6.5-x86_64/postgresql95-server-9.5.3-1PGDG.rhel6.x86_64.rpm
[root@test02 soft_bak]# wget http://yum.postgresql.org/9.5/redhat/rhel-6.5-x86_64/postgresql95-contrib-9.5.3-1PGDG.rhel6.x86_64.rpm
[root@test02 soft_bak]# wget http://yum.postgresql.org/9.5/redhat/rhel-6.5-x86_64/postgresql95-libs-9.5.3-1PGDG.rhel6.x86_64.rpm
[root@test02 soft_bak]# wget http://yum.postgresql.org/9.5/redhat/rhel-6.5-x86_64/postgresql95-9.5.3-1PGDG.rhel6.x86_64.rpm
3. 安裝PostgreSQL,注意安裝順序
[root@test02 soft_bak]# rpm -ivh postgresql95-libs-9.5.3-1PGDG.rhel6.x86_64.rpm
warning: postgresql95-libs-9.5.3-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing... ########################################### [100%]
1:postgresql95-libs ########################################### [100%]
[root@test02 soft_bak]# rpm -ivh postgresql95-9.5.3-1PGDG.rhel6.x86_64.rpm
warning: postgresql95-9.5.3-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing... ########################################### [100%]
1:postgresql95 ########################################### [100%]
[root@test02 soft_bak]# rpm -ivh postgresql95-server-9.5.3-1PGDG.rhel6.x86_64.rpm
warning: postgresql95-server-9.5.3-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing... ########################################### [100%]
1:postgresql95-server ########################################### [100%]
[root@test02 soft_bak]# rpm -ivh postgresql95-contrib-9.5.3-1PGDG.rhel6.x86_64.rpm
warning: postgresql95-contrib-9.5.3-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing... ########################################### [100%]
1:postgresql95-contrib ########################################### [100%]
4 初始化PostgreSQL 數據庫
初始化數據庫
# service postgresql-9.5 initdb
[root@test02 pgsql]# service postgresql-9.5 initdb
Initializing database: [ OK ]
5. 啓動服務
# service postgresql-9.5 start
[root@test02 pgsql]# service postgresql-9.5 start
Starting postgresql-9.5 service: [ OK ]
6. 把PostgreSQL 服務加入到啓動列表
# chkconfig postgresql-9.2 on
# chkconfig --list|grep postgres
chkconfig –list|grep postgres
7. 修改PostgreSQL 數據庫用戶postgres的密碼
PostgreSQL 數據庫默認會建立一個postgres的數據庫用戶做爲數據庫的管理員,默認密碼爲空,咱們須要修改成指定的密碼,這裏設定爲’postgres’。
[root@test02 bin]# su - postgres
-bash-4.1$ pwd
/var/lib/pgsql
-bash-4.1$ psql
psql (8.4.18, server 9.5.3)
WARNING: psql version 8.4, server version 9.5.
Some psql features might not work.
Type "help" for help.
postgres=#
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
postgres=# SELECT * from pg_shadow ;
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | us
econfig
----------+----------+-------------+----------+---------+--------------+-------------------------------------+----------+---
--------
postgres | 10 | t | t | t | t | md53175bce1d3201d16594cebf9d7eb3f9d | |
(1 row)
8. 測試數據庫
postgres=# CREATE DATABASE t_pgdb;
CREATE DATABASE
postgres=# \l+
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges | Size | Tablespace |
Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+---------------
-----------------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 7248 kB | pg_default | default admini
strative connection database
t_pgdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 7129 kB | pg_default |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres | 7129 kB | pg_default | unmodifiable e
mpty database
: postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres | 7129 kB | pg_default | default templa
te for new databases
: postgres=CTc/postgres
(4 rows)
切換到t_pgdb 數據庫
postgres=# \c t_pgdb
psql (8.4.18, server 9.5.3)
WARNING: psql version 8.4, server version 9.5.
Some psql features might not work.
You are now connected to database "t_pgdb".
t_pgdb=# CREATE TABLE t_table(id int primary key,name text);
CREATE TABLE
t_pgdb=# INSERT INTO t_table VALUES (1,'Andrew');
INSERT 0 1
t_pgdb=# INSERT INTO t_table VALUES (2,'Tonny');
INSERT 0 1
t_pgdb=# SELECT * from t_table ;
id | name
----+--------
1 | Andrew
2 | Tonny
(2 rows)
9. 修改linux 系統用戶postgres 的密碼
PostgreSQL 數據庫默認會建立一個linux 系統用戶postgres,經過passwd 命令設置系統用戶的密碼爲postgres
-bash-4.1$ exit
logout
[root@test02 bin]# passwd postgres
Changing password for user postgres.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
修改postgresql.conf文件
若是想讓PostgreSQL 監聽整個網絡的話,將listen_addresses 前的#去掉,並將 listen_addresses = 'localhost' 改爲 listen_addresses = '*',由於localhost只容許本機鏈接,#開頭的參數爲默認配置
[root@test02 bin]# vim /var/lib/pgsql/9.5/data/postgresql.conf
listen_addresses = '*'
修改客戶端認證配置文件pg_hba.conf
將須要遠程訪問數據庫的IP地址或地址段加入該文件
host all all 0.0.0.0/0 md5
[root@test02 bin]# su - postgres
-bash-4.1$ service postgresql-9.5 restart
Stopping postgresql-9.5 service: [FAILED]
touch: cannot touch `/var/lock/subsys/postgresql-9.5': Permission denied
/etc/init.d/postgresql-9.5: line 170: /var/run/postgresql-9.5.pid: Permission denied
service postgresql-9.5 restart ( root用戶執行)
[root@test02 9.5]# /etc/init.d/postgresql-9.5 restart
Stopping postgresql-9.5 service: [ OK ]
Starting postgresql-9.5 service: [ OK ]
/usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data/ stop
waiting for server to shut down.... done
server stopped
-bash-4.1$ /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data/ start
server starting
-bash-4.1$ < 2016-06-02 23:49:28.415 CST >LOG: redirecting log output to logging collector process
< 2016-06-02 23:49:28.415 CST >HINT: Future log output will appear in directory "pg_log".
-bash-4.1$ ps hf -u postgres -o cmd
-bash
\_ ps hf -u postgres -o cmd
/usr/pgsql-9.5/bin/postgres -D /var/lib/pgsql/9.5/data
\_ postgres: logger process
\_ postgres: checkpointer process
\_ postgres: writer process
\_ postgres: wal writer process
\_ postgres: autovacuum launcher process
\_ postgres: stats collector process
-bash-4.1$ /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data/ stop
waiting for server to shut down.... done
server stopped
[root@test02 bin]# rpm -qa|grep postgres
postgresql95-9.5.3-1PGDG.rhel6.x86_64
postgresql-8.4.18-1.el6_4.x86_64
postgresql-devel-8.4.18-1.el6_4.x86_64
postgresql95-server-9.5.3-1PGDG.rhel6.x86_64
postgresql95-contrib-9.5.3-1PGDG.rhel6.x86_64
postgresql-libs-8.4.18-1.el6_4.x86_64
postgresql95-libs-9.5.3-1PGDG.rhel6.x86_64
卸載(注意順序)
[root@test02 bin]# rpm -e postgresql95-server-9.5.3-1PGDG.rhel6.x86_64
[root@test02 bin]# rpm -e postgresql95-contrib-9.5.3-1PGDG.rhel6.x86_64
[root@test02 bin]# rpm -e postgresql95-9.5.3-1PGDG.rhel6.x86_64
[root@test02 bin]# rpm -e postgresql95-libs-9.5.3-1PGDG.rhel6.x86_64
YUM
若是是默認yum 安裝的話,會安裝較低版本的PostgreSQL 8.4,這不符合咱們的要求
咱們使用PostgreSQL Yum Repository 來安裝最新版本的PostgreSQL
To use the yum repository, you must first install the repository RPM. To do this, download the correct RPM from the repository RPM listing, and install it with commands like:
[root@test02 bin]# rpm -i https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-6-x86_64/pgdg-redhat95-9.5-2.noarch.rpm
Once this is done, you can proceed to install and update packages the same way as the ones included in the distribution.
[root@test02 bin]# yum install postgresql95-server postgresql95-contrib
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
pgdg95 | 3.7 kB 00:00
pgdg95/primary_db | 138 kB 00:01
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package postgresql95-contrib.x86_64 0:9.5.3-2PGDG.rhel6 will be installed
--> Processing Dependency: postgresql95-libs(x86-64) = 9.5.3-2PGDG.rhel6 for package: postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64
--> Processing Dependency: postgresql95(x86-64) = 9.5.3-2PGDG.rhel6 for package: postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64
---> Package postgresql95-server.x86_64 0:9.5.3-2PGDG.rhel6 will be installed
--> Running transaction check
---> Package postgresql95.x86_64 0:9.5.3-2PGDG.rhel6 will be installed
---> Package postgresql95-libs.x86_64 0:9.5.3-2PGDG.rhel6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================
Installing:
postgresql95-contrib x86_64 9.5.3-2PGDG.rhel6 pgdg95 456 k
postgresql95-server x86_64 9.5.3-2PGDG.rhel6 pgdg95 4.5 M
Installing for dependencies:
postgresql95 x86_64 9.5.3-2PGDG.rhel6 pgdg95 1.3 M
postgresql95-libs x86_64 9.5.3-2PGDG.rhel6 pgdg95 205 k
Transaction Summary
======================================================================================================================================
Install 4 Package(s)
Total download size: 6.5 M
Installed size: 25 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): postgresql95-9.5.3-2PGDG.rhel6.x86_64.rpm | 1.3 MB 00:27
(2/4): postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64.rpm | 456 kB 00:15
(3/4): postgresql95-libs-9.5.3-2PGDG.rhel6.x86_64.rpm | 205 kB 00:06
(4/4): postgresql95-server-9.5.3-2PGDG.rhel6.x86_64.rpm | 4.5 MB 01:32
--------------------------------------------------------------------------------------------------------------------------------------
Total 45 kB/s | 6.5 MB 02:26
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : postgresql95-libs-9.5.3-2PGDG.rhel6.x86_64 1/4
Installing : postgresql95-9.5.3-2PGDG.rhel6.x86_64 2/4
Installing : postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64 3/4
Installing : postgresql95-server-9.5.3-2PGDG.rhel6.x86_64 4/4
Verifying : postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64 1/4
Verifying : postgresql95-server-9.5.3-2PGDG.rhel6.x86_64 2/4
Verifying : postgresql95-libs-9.5.3-2PGDG.rhel6.x86_64 3/4
Verifying : postgresql95-9.5.3-2PGDG.rhel6.x86_64 4/4
Installed:
postgresql95-contrib.x86_64 0:9.5.3-2PGDG.rhel6 postgresql95-server.x86_64 0:9.5.3-2PGDG.rhel6
Dependency Installed:
postgresql95.x86_64 0:9.5.3-2PGDG.rhel6 postgresql95-libs.x86_64 0:9.5.3-2PGDG.rhel6
Complete!
查看安裝
[root@test02 bin]# rpm -qa|grep postgres
postgresql95-9.5.3-2PGDG.rhel6.x86_64
postgresql95-server-9.5.3-2PGDG.rhel6.x86_64
postgresql95-libs-9.5.3-2PGDG.rhel6.x86_64
postgresql95-contrib-9.5.3-2PGDG.rhel6.x86_64
初始化並啓動數據庫(root用戶)
[root@test02 9.5]# /etc/init.d/postgresql-9.5 initdb
Initializing database: [ OK ]
[root@test02 9.5]# /etc/init.d/postgresql-9.5 start
Starting postgresql-9.5 service: [ OK ]
[root@test02 9.5]# ps hf -u postgres -o cmd
/usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/data
\_ postgres: logger process
\_ postgres: checkpointer process
\_ postgres: writer process
\_ postgres: wal writer process
\_ postgres: autovacuum launcher process
\_ postgres: stats collector process
[root@test02 9.5]# su - postgres
-bash-4.1$ psql
psql (9.5.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
源碼安裝
1.下載源代碼
[root@test02 ~]# wget https://ftp.postgresql.org/pub/source/v9.5.3/postgresql-9.5.3.tar.gz
--2016-06-03 00:29:29-- https://ftp.postgresql.org/pub/source/v9.5.3/postgresql-9.5.3.tar.gz
Resolving ftp.postgresql.org... 174.143.35.246, 213.189.17.228, 217.196.149.55, ...
Connecting to ftp.postgresql.org|174.143.35.246|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24134415 (23M) [application/x-gzip]
Saving to: ?.ostgresql-9.5.3.tar.gz?
100%[============================================================================================>] 24,134,415 37.3K/s in 9m 58s
2016-06-03 00:39:29 (39.4 KB/s) - ?.ostgresql-9.5.3.tar.gz?.saved [24134415/24134415]
2.解壓PostgreSQL源碼包 :tar zxvf postgresql-9.2.4.tar.gz
或 tar jxvf postgresql-9.5.3.tar.bz2
3,切換到剛剛解壓的目錄下:cd postgresql-9.5.3
4. ./configure
若是遇到錯誤,則須要以下安裝依賴工具包(按需安裝)
yum install gcc
yum install readline
yum install flex
yum install zlib
在執行./configure
gmake world
gmake install-world
安裝完畢
而後啓動數據庫
到安裝目錄(默認爲/usr/local/postgres)
在該目錄下創建data目錄 (存放數據庫相關文件)
啓動數據庫須要非root用戶
/usr/local/pgsql//bin
初始化數據庫
./initdb -D ../data start
啓動數據庫
./pg_clt -D ../data start
啓動後鏈接數據庫:
在/usrl/local/pgsql/bin目錄下
執行 ./psql 鏈接到默認的postgres數據
而後能夠建立數據庫,建立表,等操做了。
PostgreSQL run文件安裝
[root@test01 home]# cd /opt/soft/
[root@test01 soft]# ll
total 97804
-rw-r--r--. 1 root root 38101062 May 27 17:57 postgresql-9.5.3-1-linux-x64.run
-rw-r--r--. 1 root root 37116313 May 27 18:05 postgresql-9.6.0-beta1-linux-x64.run
-rw-r--r--. 1 500 500 24925549 May 25 01:29 postgresql-9.6beta1.tar.gz
[root@test01 soft]# chmod +x postgresql-9.5.3-1-linux-x64.run
[root@test01 soft]# ll
total 97804
-rwxr-xr-x. 1 root root 38101062 May 27 17:57 postgresql-9.5.3-1-linux-x64.run
-rw-r--r--. 1 root root 37116313 May 27 18:05 postgresql-9.6.0-beta1-linux-x64.run
-rw-r--r--. 1 500 500 24925549 May 25 01:29 postgresql-9.6beta1.tar.gz
[root@test01 soft]# ./postgresql-9.5.3-1-linux-x64.run
or
[root@test01 soft]# ./postgresql-9.5.3-1-linux-x64.run --mode text
......