新聞中心
PostgreSQL 是一種非常復(fù)雜的對(duì)象-關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(ORDBMS),也是目前功能最強(qiáng)大,特性最豐富和最復(fù)雜的自由軟件數(shù)據(jù)庫(kù)系統(tǒng)。
os:centos6.5 x64
ip:192.168.85.130
hostname: vm2.lansgg.com
pg 版本:postgresql-9.2.4.tar.bz2
一、yum安裝
二、源碼安裝
三、系統(tǒng)數(shù)據(jù)庫(kù)
1、yum安裝
[root@vm2 ~]# wget [root@vm2 ~]# rpm -vhi pgdg-redhat92-9.2-8.noarch.rpm [root@vm2 ~]# yum install postgresql92-server postgresql92-contrib -y1.2、初始化并啟動(dòng)數(shù)據(jù)庫(kù)
[root@vm2 ~]# /etc/init.d/postgresql-9.2 initdb 正在初始化數(shù)據(jù)庫(kù): [確定] [root@vm2 ~]# /etc/init.d/postgresql-9.2 start 啟動(dòng) postgresql-9.2 服務(wù): [確定][root@vm2 ~]# echo "PATH=/usr/pgsql-9.2/bin:$PATH" >> /etc/profile [root@vm2 ~]# echo "export PATH" >> /etc/profile1.3、測(cè)試
[root@vm2 ~]# su - postgres -bash-4.1$ psql psql (9.2.19) 輸入 "help" 來獲取幫助信息. postgres=# \l 資料庫(kù)列表 名稱 | 擁有者 | 字元編碼 | 校對(duì)規(guī)則 | Ctype | 存取權(quán)限 -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 行記錄) postgres=#1.4、修改管理員密碼
修改PostgreSQL 數(shù)據(jù)庫(kù)用戶postgres的密碼(注意不是linux系統(tǒng)帳號(hào))
PostgreSQL 數(shù)據(jù)庫(kù)默認(rèn)會(huì)創(chuàng)建一個(gè)postgres的數(shù)據(jù)庫(kù)用戶作為數(shù)據(jù)庫(kù)的管理員,默認(rèn)密碼為空,我們需要修改為指定的密碼,這里設(shè)定為’postgres’。
1.5、創(chuàng)建測(cè)試數(shù)據(jù)庫(kù)
postgres=# create database testdb; CREATE DATABASE postgres=# \c testdb; 您現(xiàn)在已經(jīng)連線到數(shù)據(jù)庫(kù) "testdb",用戶 "postgres". testdb=#1.6、創(chuàng)建測(cè)試表
testdb=# create table test (id integer, name text); CREATE TABLE testdb=# insert into test values(1,'lansgg'); INSERT 0 1 testdb=# select * from test; id | name ----+-------- 1 | lansgg (1 行記錄)1.7、查看表結(jié)構(gòu)
testdb=# \d test; 資料表 "public.test" 欄位 | 型別 | 修飾詞 ------+---------+-------- id | integer | name | text |1.8、修改PostgresSQL 數(shù)據(jù)庫(kù)配置實(shí)現(xiàn)遠(yuǎn)程訪問
修改postgresql.conf 文件
如果想讓PostgreSQL 監(jiān)聽整個(gè)網(wǎng)絡(luò)的話,將listen_addresses 前的#去掉,并將 listen_addresses = 'localhost' 改成 listen_addresses = '*'
修改客戶端認(rèn)證配置文件pg_hba.conf
2、源碼安裝
停止上面yum安裝的pgsql服務(wù),下載PostgreSQL 源碼包
[root@vm2 ~]# /etc/init.d/postgresql-9.2 stop 停止 postgresql-9.2 服務(wù): [確定] [root@vm2 ~]# wget [root@vm2 ~]# tar jxvf postgresql-9.2.4.tar.bz2 [root@vm2 ~]# cd postgresql-9.2.4查看INSTALL 文件
more INSTALL
INSTALL 文件中Short Version 部分解釋了如何安裝PostgreSQL 的命令,Requirements 部分描述了安裝PostgreSQL 所依賴的lib,比較長(zhǎng),先configure 試一下,如果出現(xiàn)error,那么需要檢查是否滿足了Requirements 的要求。
開始編譯安裝PostgreSQL 數(shù)據(jù)庫(kù)。
2.2、初始化數(shù)據(jù)庫(kù)
useradd -d /opt/postgres postgres ######如果沒有此賬戶就創(chuàng)建,前面yum安裝的時(shí)候已經(jīng)替我們創(chuàng)建了 [root@vm2 postgresql-9.2.4]# mkdir /usr/local/pgsql/data [root@vm2 postgresql-9.2.4]# chown postgres.postgres /usr/local/pgsql/data/ [root@vm2 postgresql-9.2.4]# su - postgres -bash-4.1$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/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 "zh_CN.UTF-8". The default database encoding has accordingly been set to "UTF8". initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8" The default text search configuration will be set to "simple". fixing permissions on existing directory /usr/local/pgsql/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 32MB creating configuration files ... ok creating template1 database in /usr/local/pgsql/data/base/1 ... ok initializing pg_authid ... 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 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 -bash-4.1$1.3、添加到系統(tǒng)服務(wù)
-bash-4.1$ exit logout [root@vm2 postgresql-9.2.4]# cp /root/postgresql-9.2.4/contrib/start-scripts/linux /etc/init.d/postgresql [root@vm2 postgresql-9.2.4]# chmod +x /etc/init.d/postgresql [root@vm2 postgresql-9.2.4]# /etc/init.d/postgresql start Starting PostgreSQL: ok [root@vm2 postgresql-9.2.4]# chkconfig --add postgresql [root@vm2 postgresql-9.2.4]# chkconfig postgresql on [root@vm2 postgresql-9.2.4]#1.4、測(cè)試使用
[root@vm2 postgresql-9.2.4]# su - postgres -bash-4.1$ psql -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) -bash-4.1$ psql psql (9.2.4) Type "help" for help. postgres=# create database testdb; CREATE DATABASE postgres=# \c testdb; You are now connected to database "testdb" as user "postgres". testdb=# create table test(id int,name text,age int); CREATE TABLE testdb=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | age | integer | testdb=# insert into test values(1,'lansgg',25); INSERT 0 1 testdb=# select * from test; id | name | age ----+--------+----- 1 | lansgg | 25 (1 row) testdb=#3、系統(tǒng)數(shù)據(jù)庫(kù)
在創(chuàng)建數(shù)據(jù)集簇之后,該集簇中默認(rèn)包含三個(gè)系統(tǒng)數(shù)據(jù)庫(kù)template1、template0和postgres。其中template0和postgres都是在初始化過程中從template1拷貝而來的。
template1和template0數(shù)據(jù)庫(kù)用于創(chuàng)建數(shù)據(jù)庫(kù)。PostgreSQL中采用從模板數(shù)據(jù)庫(kù)復(fù)制的方式來創(chuàng)建新的數(shù)據(jù)庫(kù),在創(chuàng)建數(shù)據(jù)庫(kù)的命令中可以用“-T”選項(xiàng)來指定以哪個(gè)數(shù)據(jù)庫(kù)為模板來創(chuàng)建新數(shù)據(jù)庫(kù)。
template1數(shù)據(jù)庫(kù)是創(chuàng)建數(shù)據(jù)庫(kù)命令默認(rèn)的模板,也就是說通過不帶“-T”選項(xiàng)的命令創(chuàng)建的用戶數(shù)據(jù)庫(kù)是和template1一模一樣的。template1是可以修改的,如果對(duì)template1進(jìn)行了修改,那么在修改之后創(chuàng)建的用戶數(shù)據(jù)庫(kù)中也能體現(xiàn)出這些修改的結(jié)果。template1的存在允許用戶可以制作一個(gè)自定義的模板數(shù)據(jù)庫(kù),在其中用戶可以創(chuàng)建一些應(yīng)用需要的表、數(shù)據(jù)、索引等,在日后需要多次創(chuàng)建相同內(nèi)容的數(shù)據(jù)庫(kù)時(shí),都可以用template1作為模板生成。
由于template1的內(nèi)容有可能被用戶修改,因此為了滿足用戶創(chuàng)建一個(gè)“干凈”數(shù)據(jù)庫(kù)的需求,PostgreSQL提供了template0數(shù)據(jù)庫(kù)作為最初始的備份數(shù)據(jù),當(dāng)需要時(shí)可以用template0作為模板生成“干凈”的數(shù)據(jù)庫(kù)。
而第三個(gè)初始數(shù)據(jù)庫(kù)postgres用于給初始用戶提供一個(gè)可連接的數(shù)據(jù)庫(kù),就像Linux系統(tǒng)中一個(gè)用戶的主目錄一樣。
上述系統(tǒng)數(shù)據(jù)庫(kù)都是可以刪除的,但是兩個(gè)模板數(shù)據(jù)庫(kù)在刪除之前必須將其在pg_database中元組的datistemplate屬性改為FALSE,否則刪除時(shí)會(huì)提示“不能刪除一個(gè)模板數(shù)據(jù)庫(kù)”
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享名稱:Postgresql服務(wù)部署-創(chuàng)新互聯(lián)
鏈接地址:http://www.dlmjj.cn/article/eshdd.html