Postgresql服務部署

來源:互聯網
上載者:User

標籤:postgresql   pgsql   postregs   

PostgreSQL 是一種非常複雜的對象-關係型資料庫管理系統(ORDBMS),也是目前功能最強大,特性最豐富和最複雜的自由軟體資料庫系統。
os:centos6.5 x64

ip:192.168.85.130

hostname: vm2.lansgg.com

pg 版本:postgresql-9.2.4.tar.bz2


一、yum安裝
二、源碼安裝

三、系統資料庫



1、yum安裝

[[email protected] ~]# wget [[email protected] ~]# rpm -vhi pgdg-redhat92-9.2-8.noarch.rpm[[email protected] ~]# yum install postgresql92-server postgresql92-contrib -y

1.2、初始化並啟動資料庫

[[email protected] ~]# /etc/init.d/postgresql-9.2 initdb正在初始化資料庫:                                         [確定][[email protected] ~]# /etc/init.d/postgresql-9.2 start啟動 postgresql-9.2 服務:                                 [確定]
[[email protected] ~]# echo "PATH=/usr/pgsql-9.2/bin:$PATH" >> /etc/profile[[email protected] ~]# echo "export PATH" >> /etc/profile

1.3、測試

[[email protected] ~]# su - postgres-bash-4.1$ psqlpsql (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.4、修改管理員密碼

修改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 ROLEpostgres=# select * from pg_shadow; usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |               passwd                | valuntil | useconfig ----------+----------+-------------+----------+-----------+---------+-------------------------------------+----------+----------- postgres |       10 | t           | t        | t         | t       | md53175bce1d3201d16594cebf9d7eb3f9d |          | (1 行記錄)postgres=#

1.5、建立測試資料庫

postgres=# create database testdb;CREATE DATABASEpostgres=# \c testdb;您現在已經連線到資料庫 "testdb",使用者 "postgres".testdb=#

1.6、建立測試表

testdb=#  create table test (id integer, name text);CREATE TABLEtestdb=# insert into test values(1,‘lansgg‘);INSERT 0 1testdb=# select * from test; id |  name  ----+--------  1 | lansgg(1 行記錄)

1.7、查看錶結構

testdb=# \d test;  資料表 "public.test" 欄位 |  型別   | 修飾詞 ------+---------+-------- id   | integer |  name | text    |

1.8、修改PostgresSQL 資料庫配置實現遠端存取

修改postgresql.conf 檔案

如果想讓PostgreSQL 監聽整個網路的話,將listen_addresses 前的#去掉,並將 listen_addresses = ‘localhost‘ 改成 listen_addresses = ‘*‘
修改用戶端認證設定檔pg_hba.conf

[[email protected] ~]# vim /var/lib/pgsql/9.2/data/pg_hba.confhost    all             all             127.0.0.1/32            identhost    all             all             all                     md5
[[email protected] ~]# /etc/init.d/postgresql-9.2 restart停止 postgresql-9.2 服務:                                 [確定]啟動 postgresql-9.2 服務:                                 [確定][[email protected] ~]#

2、源碼安裝

停止上面yum安裝的pgsql服務,下載PostgreSQL 源碼包

[[email protected] ~]# /etc/init.d/postgresql-9.2 stop停止 postgresql-9.2 服務:                                 [確定][[email protected] ~]# wget [[email protected] ~]# tar jxvf postgresql-9.2.4.tar.bz2[[email protected] ~]# cd postgresql-9.2.4

查看INSTALL 檔案
more INSTALL
INSTALL 檔案中Short Version 部分解釋了如何安裝PostgreSQL 的命令,Requirements 部分描述了安裝PostgreSQL 所依賴的lib,比較長,先configure 試一下,如果出現error,那麼需要檢查是否滿足了Requirements 的要求。
開始編譯安裝PostgreSQL 資料庫。

[[email protected] postgresql-9.2.4]# ./configure[[email protected] postgresql-9.2.4]# gmake[[email protected] postgresql-9.2.4]# gmake install[[email protected] postgresql-9.2.4]# echo "PGHOME=/usr/local/pgsql" >> /etc/profile[[email protected] postgresql-9.2.4]# echo "export PGHOME" >> /etc/profile[[email protected] postgresql-9.2.4]# echo "PGDATA=/usr/local/pgsql/data" >> /etc/profile[[email protected] postgresql-9.2.4]# echo "export PGDATA" >> /etc/profile[[email protected] postgresql-9.2.4]# echo "PATH=$PGHOME/bin:$PATH" >> /etc/profile[[email protected] postgresql-9.2.4]# echo "export PATH" >> /etc/profile[[email protected] postgresql-9.2.4]# source /etc/profile[[email protected] postgresql-9.2.4]#

2.2、初始化資料庫

useradd -d /opt/postgres postgres              ######如果沒有此賬戶就建立,前面yum安裝的時候已經替我們建立了[[email protected] postgresql-9.2.4]# mkdir /usr/local/pgsql/data[[email protected] postgresql-9.2.4]# chown postgres.postgres /usr/local/pgsql/data/[[email protected] 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 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 32MBcreating configuration files ... okcreating template1 database in /usr/local/pgsql/data/base/1 ... okinitializing pg_authid ... okinitializing dependencies ... okcreating system views ... okloading system objects‘ descriptions ... okcreating collations ... okcreating conversions ... okcreating dictionaries ... oksetting privileges on built-in objects ... okcreating information schema ... okloading PL/pgSQL server-side language ... okvacuuming database template1 ... okcopying template1 to template0 ... okcopying template1 to postgres ... okWARNING: enabling "trust" authentication for local connectionsYou 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/dataor    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start-bash-4.1$

1.3、添加到系統服務

-bash-4.1$ exitlogout[[email protected] postgresql-9.2.4]#  cp /root/postgresql-9.2.4/contrib/start-scripts/linux /etc/init.d/postgresql[[email protected] postgresql-9.2.4]# chmod +x /etc/init.d/postgresql[[email protected] postgresql-9.2.4]# /etc/init.d/postgresql startStarting PostgreSQL: ok[[email protected] postgresql-9.2.4]# chkconfig --add postgresql[[email protected] postgresql-9.2.4]# chkconfig postgresql on[[email protected] postgresql-9.2.4]#

1.4、測試使用

[[email protected] 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$ psqlpsql (9.2.4)Type "help" for help.postgres=# create database testdb;CREATE DATABASEpostgres=# \c testdb;You are now connected to database "testdb" as user "postgres".testdb=# create table test(id int,name text,age int);CREATE TABLEtestdb=# \d test     Table "public.test" Column |  Type   | Modifiers --------+---------+----------- id     | integer |  name   | text    |  age    | integer | testdb=# insert into test values(1,‘lansgg‘,25);INSERT 0 1testdb=# select * from test; id |  name  | age ----+--------+-----  1 | lansgg |  25(1 row)testdb=#

3、系統資料庫


 在建立資料集簇之後,該集簇中預設包含三個系統資料庫template1、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,否則刪除時會提示“不能刪除一個模板資料庫”

本文出自 “大風” 部落格,請務必保留此出處http://lansgg.blog.51cto.com/5675165/1888614

Postgresql服務部署

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.