PostgreSQL 是一種很是複雜的對象-關係型數據庫管理系統(ORDBMS),也是目前功能最強大,特性最豐富和最複雜的自由軟件數據庫系統。
os:centos6.5 x64linux
ip:192.168.85.130sql
hostname: vm2.lansgg.com數據庫
pg 版本:postgresql-9.2.4.tar.bz2vim
1、yum安裝
2、源碼安裝centos
3、系統數據庫bash
一、yum安裝網絡
[root@vm2 ~]# wget [root@vm2 ~]# rpm -vhi pgdg-redhat92-9.2-8.noarch.rpm [root@vm2 ~]# yum install postgresql92-server postgresql92-contrib -y
1.二、初始化並啓動數據庫ide
[root@vm2 ~]# /etc/init.d/postgresql-9.2 initdb 正在初始化數據庫: [肯定] [root@vm2 ~]# /etc/init.d/postgresql-9.2 start 啓動 postgresql-9.2 服務: [肯定]
[root@vm2 ~]# echo "PATH=/usr/pgsql-9.2/bin:$PATH" >> /etc/profile [root@vm2 ~]# echo "export PATH" >> /etc/profile
1.三、測試post
[root@vm2 ~]# su - postgres -bash-4.1$ psql psql (9.2.19) 輸入 "help" 來獲取幫助信息. postgres=# \l 資料庫列表 名稱 | 擁有者 | 字元編碼 | 校對規則 | Ctype | 存取權限 -----------+----------+----------+-------------+-------------+----------------------- 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.四、修改管理員密碼
測試
修改PostgreSQL 數據庫用戶postgres的密碼(注意不是linux系統賬號)
PostgreSQL 數據庫默認會建立一個postgres的數據庫用戶做爲數據庫的管理員,默認密碼爲空,咱們須要修改成指定的密碼,這裏設定爲’postgres’。
postgres=# select * from pg_shadow; usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig ----------+----------+-------------+----------+-----------+---------+--------+----------+----------- postgres | 10 | t | t | t | t | | | (1 行記錄) postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; ALTER ROLE postgres=# select * from pg_shadow; usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig ----------+----------+-------------+----------+-----------+---------+-------------------------------------+----------+----------- postgres | 10 | t | t | t | t | md53175bce1d3201d16594cebf9d7eb3f9d | | (1 行記錄) postgres=#
1.五、建立測試數據庫
postgres=# create database testdb; CREATE DATABASE postgres=# \c testdb; 您如今已經連線到數據庫 "testdb",用戶 "postgres". testdb=#
1.六、建立測試表
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.七、查看錶結構
testdb=# \d test; 資料表 "public.test" 欄位 | 型別 | 修飾詞 ------+---------+-------- id | integer | name | text |
1.八、修改PostgresSQL 數據庫配置實現遠程訪問
修改postgresql.conf 文件
若是想讓PostgreSQL 監聽整個網絡的話,將listen_addresses 前的#去掉,並將 listen_addresses = 'localhost' 改爲 listen_addresses = '*'
修改客戶端認證配置文件pg_hba.conf
[root@vm2 ~]# vim /var/lib/pgsql/9.2/data/pg_hba.conf host all all 127.0.0.1/32 ident host all all all md5
[root@vm2 ~]# /etc/init.d/postgresql-9.2 restart 中止 postgresql-9.2 服務: [肯定] 啓動 postgresql-9.2 服務: [肯定] [root@vm2 ~]#
二、源碼安裝
中止上面yum安裝的pgsql服務,下載PostgreSQL 源碼包
[root@vm2 ~]# /etc/init.d/postgresql-9.2 stop 中止 postgresql-9.2 服務: [肯定] [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,比較長,先configure 試一下,若是出現error,那麼須要檢查是否知足了Requirements 的要求。
開始編譯安裝PostgreSQL 數據庫。
[root@vm2 postgresql-9.2.4]# ./configure [root@vm2 postgresql-9.2.4]# gmake [root@vm2 postgresql-9.2.4]# gmake install [root@vm2 postgresql-9.2.4]# echo "PGHOME=/usr/local/pgsql" >> /etc/profile [root@vm2 postgresql-9.2.4]# echo "export PGHOME" >> /etc/profile [root@vm2 postgresql-9.2.4]# echo "PGDATA=/usr/local/pgsql/data" >> /etc/profile [root@vm2 postgresql-9.2.4]# echo "export PGDATA" >> /etc/profile [root@vm2 postgresql-9.2.4]# echo "PATH=$PGHOME/bin:$PATH" >> /etc/profile [root@vm2 postgresql-9.2.4]# echo "export PATH" >> /etc/profile [root@vm2 postgresql-9.2.4]# source /etc/profile [root@vm2 postgresql-9.2.4]#
2.二、初始化數據庫
useradd -d /opt/postgres postgres ######若是沒有此帳戶就建立,前面yum安裝的時候已經替咱們建立了 [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.三、添加到系統服務
-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.四、測試使用
[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=#
三、系統數據庫
在建立數據集簇以後,該集簇中默認包含三個系統數據庫template一、template0和postgres。其中template0和postgres都是在初始化過程當中從template1拷貝而來的。 template1和template0數據庫用於建立數據庫。PostgreSQL中採用從模板數據庫複製的方式來建立新的數據庫,在建立數據庫的命令中能夠用「-T」選項來指定以哪一個數據庫爲模板來建立新數據庫。 template1數據庫是建立數據庫命令默認的模板,也就是說經過不帶「-T」選項的命令建立的用戶數據庫是和template1如出一轍的。template1是能夠修改的,若是對template1進行了修改,那麼在修改以後建立的用戶數據庫中也能體現出這些修改的結果。template1的存在容許用戶能夠製做一個自定義的模板數據庫,在其中用戶能夠建立一些應用須要的表、數據、索引等,在往後須要屢次建立相同內容的數據庫時,均可以用template1做爲模板生成。 因爲template1的內容有可能被用戶修改,所以爲了知足用戶建立一個「乾淨」數據庫的需求,PostgreSQL提供了template0數據庫做爲最初始的備份數據,當須要時能夠用template0做爲模板生成「乾淨」的數據庫。 而第三個初始數據庫postgres用於給初始用戶提供一個可鏈接的數據庫,就像Linux系統中一個用戶的主目錄同樣。 上述系統數據庫都是能夠刪除的,可是兩個模板數據庫在刪除以前必須將其在pg_database中元組的datistemplate屬性改成FALSE,不然刪除時會提示「不能刪除一個模板數據庫」