<b>2.1 從發行版本安裝</b>
<b>2.1.1 在debian或ubuntu下的安裝</b>
在debian和ubuntu下一般使用apt-get指令或aptitude指令來安裝軟體,指令如下:
sudo apt-get install postgresql
安裝完畢後,postgresql資料庫就啟動了,如果想進入資料庫,需要切換到postgres使用者下:
su - postgres
然後使用psql連接配接到資料庫中,從作業系統下的“postgres”使用者(注意這裡指的不是資料庫中的postgres使用者)連接配接資料庫是不需要密碼的,如下:
root@osdba-laptop:~# sudo su - postgres
postgres@osdba-laptop:~$ psql
psql (9.1.9)
type "help" for help.
postgres=# \l
list of
databases
name | owner
| encoding | collate |
ctype | access
privileges
-----------+----------+----------+------------+------------+--------------------
postgres
| postgres | utf8 | en_us.utf8
| en_us.utf8 |
template0
| postgres | utf8 | en_us.utf8
| en_us.utf8 | =c/postgres
+
| | | | |
postgres=ctc/postgres
template1 | postgres | utf8 | en_us.utf8 | en_us.utf8 |
=c/postgres
| | | | |
(3 rows)
postgres=# \q
在上面的示例中,使用psql指令連接配接postgresql資料庫,psql是postgresql中的用戶端工具。“\l”是列出所有資料庫的指令,“\q”是退出psql的指令,在後面的章節中還會詳細介紹psql的使用方法。
在debian或ubuntu下,使用apt-get安裝完成的postgresql資料庫的資料目錄在/var/lib/postgresql/<dbversion>/main目錄下:
postgres@osdba-laptop:~$ cd
/var/lib/postgresql/9.1/main
postgres@osdba-laptop:~/9.1/main$ ls -l
total 56
drwx------ 5 postgres postgres 4096 jun 29
11:55 base
drwx------ 2 postgres postgres 4096 jun 29
11:55 global
11:55 pg_clog
drwx------ 4 postgres postgres 4096 jun 29
11:55 pg_multixact
11:55 pg_notify
11:55 pg_serial
12:00 pg_stat_tmp
11:55 pg_subtrans
11:55 pg_tblspc
11:55 pg_twophase
-rw------- 1 postgres postgres 4 jun 29 11:55 pg_version
drwx------ 3 postgres postgres 4096 jun 29
11:55 pg_xlog
-rw------- 1 postgres postgres 133 jun 29 11:55 postmaster.opts
-rw------- 1 postgres postgres 100 jun 29 11:55 postmaster.pid
lrwxrwxrwx 1 root root
36 jun 29 11:55 server.crt ->
/etc/ssl/certs/ssl-cert-snakeoil.pem
lrwxrwxrwx 1 root root
38 jun 29 11:55 server.key ->
/etc/ssl/private/ssl- cert-snakeoil.key
安裝完成後,可以使用linux下的服務管理指令service來啟停資料庫:
osdba@osdba-laptop:~$ sudo service
postgresql status
9.1/main (port 5432): online
postgresql stop
*
stopping postgresql 9.1 database server
[ ok ]
postgresql start
starting postgresql 9.1 database server
osdba@osdba-laptop:~$
2.1.2在redhat、centos或fedora下的安裝
在redhat、centos或fedora下可使用yum工具來安裝postgresql,但這些系統的軟體庫中自帶的postgresql版本較低,其版本情況見表2-1。
表2-1linux發行版本自帶的postgresql版本
linux發行版本 自帶的postgresql版本
rhel/centos/sl 5 8.1 (also supplies package postgresql84)
rhel/centos/sl 6 8.4
fedora 16、fedora 17 9.1
fedora 18 9.2
如果上面的postgresql版本能滿足你的需要,可按下面的步驟進行安裝,否則就參照2.3.1節中的内容進行安裝。
安裝指令如下:
yum install postgresql-server.x86_64
具體安裝過程如下:
[root@localhost ~]# yum install
postgresql-server.x86_64
loaded plugins: fastestmirror
loading mirror speeds from cached hostfile
base: mirrors.163.com
extras: mirrors.163.com
updates: mirrors.163.com
setting up install process
resolving dependencies
--> running transaction check
---> package postgresql-server.x86_64
0:8.4.13-1.el6_3 will be installed
--> processing dependency:
postgresql-libs(x86-64) = 8.4.13-1.el6_3 for package:
postgresql-server-8.4.13-1.el6_3.x86_64
postgresql(x86-64) = 8.4.13-1.el6_3 for package:
libpq.so.5()(64bit) for package: postgresql-server-8.4.13-1.el6_3.x86_64
---> package postgresql.x86_64
---> package postgresql-libs.x86_64
--> finished dependency resolution
dependencies resolved
================================================================================
package arch
version repository size
installing:
postgresql-server x86_64
8.4.13-1.el6_3 base 3.4 m
installing for dependencies:
postgresql x86_64
8.4.13-1.el6_3 base 2.8 m
postgresql-libs x86_64
8.4.13-1.el6_3 base 200 k
transaction summary
install 3 package(s)
total size: 6.4 m
installed size: 29 m
is this ok [y/n]: y
yum還會讓你選擇是否把一些依賴的包也安裝上,當然要選擇“y”:
downloading packages:
warning: rpmts_hdrfromfdno: header v3
rsa/sha1 signature, key id c105b9de: nokey
retrieving key from
file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-6
importing gpg key 0xc105b9de:
userid : centos-6 key (centos 6 official
signing key) <[email protected]>
package:
centos-release-6-4.el6.centos.10.x86_64
(@anaconda-centos-201303020151.x86_64/6.4)
from :
/etc/pki/rpm-gpg/rpm-gpg-key-centos-6
running rpm_check_debug
running transaction test
transaction test succeeded
running transaction
installing : postgresql-libs-8.4.13-1.el6_3.x86_64
1/3
installing : postgresql-8.4.13-1.el6_3.x86_64
2/3
installing : postgresql-server-8.4.13-1.el6_3.x86_64
3/3
verifying :
postgresql-libs-8.4.13-1.el6_3.x86_64
postgresql-8.4.13-1.el6_3.x86_64
installed:
postgresql-server.x86_64 0:8.4.13-1.el6_3
dependency installed:
postgresql.x86_64 0:8.4.13-1.el6_3
postgresql-libs.x86_64 0:8.4.13-1.el6_3
complete!
[root@localhost ~]#
這樣就安裝好了。
在redhat下,安裝好後,postgresql服務并沒有啟動:
[root@localhost ~]# service postgresql
status
postmaster is stopped
直接啟動會報錯:
start
/var/lib/pgsql/data is missing. use
"service postgresql initdb" to initialize the cluster first.
[failed]
上面是在提示資料庫還沒有初使用化,請按提示把資料庫初使用化:
initdb
initializing database: [ ok ]
再啟動資料庫:
postmaster (pid 1345) is running...
切換到作業系統下的“postgres”使用者,登入資料庫:
[root@localhost ~]# su - postgres
-bash-4.1$ psql
psql (8.4.13)
list of
name | owner
| encoding | collation |
-----------+----------+----------+-------------+-------------+------------------
| postgres | utf8 |
en_us.utf-8 | en_us.utf-8 |
template0 | postgres | utf8 | en_us.utf-8 | en_us.utf-8 | =c/postgres
:
template1 | postgres | utf8 | en_us.utf-8 | en_us.utf-8 | =c/postgres
:
postgres=#
這樣就可以了。
也可以使用下面的指令把第三方貢獻的軟體包安裝上:
yum install postgresql-contrib.x86_64
在redhat或centos下,預設安裝上的postgresql的資料目錄在/var/lib/pgsql/data目錄下,如下:
-bash-4.1$ ls -l /var/lib/pgsql/data
total 80
drwx------. 5 postgres postgres 4096 jun 30 11:07 base
drwx------. 2 postgres postgres 4096 jun 30 11:08 global
drwx------. 2 postgres postgres 4096 jun 30 11:07 pg_clog
-rw-------. 1 postgres postgres 3411 jun 30 11:07 pg_hba.conf
-rw-------. 1 postgres postgres 1631 jun 30 11:07 pg_ident.conf
圖 2-1 postgresql官方網站drwx------. 4 postgres postgres
4096 jun 30 11:07 pg_multixact
drwx------. 2 postgres postgres 4096 jun 30 11:13 pg_stat_tmp
drwx------. 2 postgres postgres 4096 jun 30 11:07 pg_subtrans
drwx------. 2 postgres postgres 4096 jun 30 11:07 pg_tblspc
drwx------. 2 postgres postgres 4096 jun 30 11:07 pg_twophase
-rw-------. 1 postgres postgres 4 jun 30 11:07 pg_version
drwx------. 3 postgres postgres 4096 jun 30 11:07 pg_xlog
-rw-------. 1 postgres postgres 16886 jun
30 11:07 postgresql.conf
-rw-------. 1 postgres postgres 57 jun 30 11:08 postmaster.opts
-rw-------. 1 postgres postgres 45 jun 30 11:08 postmaster.pid
<b>2.1.3 在windows下的安裝</b>
在windows安裝時,先要到官網上下載下傳postgresql的windows安裝包,如圖2-1所示。
圖 2-1 postgresql官方網站
點頁面中的“download”,進入下載下傳頁面,如圖2-2所示。
圖2-2 postgresql官方下載下傳頁面
然後選擇下載下傳安裝包的類型為“windows”,進入“windows”安裝包的下載下傳界面,如圖2-3所示。
圖2-3 postgresql windows版本下載下傳頁面
這裡需要根據你的windows是32位的還是64位來選擇下載下傳合适的安裝包,如圖2-4所示。
圖2-4 postgresql windows版本下的各種類型下載下傳頁面
因為windows版本的安裝包,是enterprise db公司制作的,是以下載下傳時會顯示enterprisedb公司的界面,如圖2-5所示。
圖2-5 postgresql windows版本下載下傳轉到enterprisedb公司頁面
如果在可以下載下傳時就選擇運作,那麼在下載下傳後,就會直接運作安裝程式,當然也可以在下載下傳完後,再輕按兩下安裝程式,如圖2-6所示。
安裝程式運作後,即會顯示出安裝向導,點“next”即可,如圖2-7所示。
進入選擇安裝目錄界面,一般選擇預設安裝目錄即可,可直接點“next”,如圖2-8所示。
進入選擇資料目錄的界面,根據實際需要,選擇具體的目錄,然後點“next”,如圖2-9所示。
進入資料庫超級使用者postgres的密碼設定界面,以後可以使用這個超級使用者增加更多的資料庫使用者,設定完後點“next”,如圖2-10所示。
然後進入選擇資料庫監聽端口的界面,如果5432這個端口沒有被别的應用程式占用,那麼可使用這個預設端口,點“next”,如圖2-11所示。
選擇語言,使用預設設定就可以了,點“next”,如圖2-12所示。
進入準備安裝的界面,點“next”,如圖2-13所示。
開始安裝,如圖2-14所示。
安裝完畢後,會問你是否需要使用“stack builder”安裝一些附加的軟體。stack builder是一個安裝postgresql附加軟體的圖形化工具。如果不需要,可以把選擇框中的勾去掉,直接結束安裝。
如果沒有去掉,則會出現“stack builder”的界面,在該界面中會提示你為哪一個postgresql安裝附加軟體(出現此提示的原因是可以安裝多個不同版本的postgresql),如圖2-16所示。
這時會出現一個可以選擇安裝附加軟體的界面,根據需要選擇了附加軟體後,點“next”,如圖2-17所示。
如果是第一次安裝,不知道要安裝哪些附加軟體,可以點取消。以後需要時可以再次運作“stack builder”安裝相應的附加軟體。
<b>2.1.4 發行版安裝總結</b>
windows下的安裝是比較簡單的,隻需要運作圖形界面,基本上在安裝的過程中點選“next”項就可以完成安裝。
linux下各發行版本則使用相應版本的包管理器來進行安裝即可。
前面曾提到,從發行版本安裝postgresql,所安裝的一般不是最新版本,如果想安裝最新版本的postgresql,則要看下面介紹的方法或見2.3.1節的技巧。