linux安裝PostgreSQL並啟動和關閉教程

來源:互聯網
上載者:User

作業系統版本


[root@web103 ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
[root@web103 ~]# uname -a
Linux web103 2.6.18-348.el5 #1 SMP Tue Jan 8 17:53:53 EST 2013 x86_64 x86_64 x86_64 GNU/Linux

下載對應PostgreSQL對應rpm包
因為作業系統版本為CentOS 5.9的64位Linux,因此下載對應版本prm包,主要下載了server,client,contrib,libs四個包


[root@web103 ~]# mkdir pg
[root@web103 ~]# cd pg
[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:44:52--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1807607 (1.7M) [application/x-redhat-package-manager]
Saving to: `postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 1,807,607   73.6K/s   in 30s     
-06-16 20:45:24 (58.1 KB/s) - `postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [1807607/1807607]

[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:45:35--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6175991 (5.9M) [application/x-redhat-package-manager]
Saving to: `postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 6,175,991   58.5K/s   in 2m 4s   
-06-16 20:47:42 (48.6 KB/s) - `postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [6175991/6175991]

[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:47:51--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664051 (648K) [application/x-redhat-package-manager]
Saving to: `postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 664,051     28.1K/s   in 53s     
-06-16 20:48:46 (12.3 KB/s) - `postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [664051/664051]

[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:51:10--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 233206 (228K) [application/x-redhat-package-manager]
Saving to: `postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 233,206     70.3K/s   in 3.2s    
-06-16 20:51:16 (70.3 KB/s) - `postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [233206/233206]

[root@web103 pg]# ls
postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm          postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm
postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm  postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm

安裝PostgreSQL rpm包


[root@web103 pg]# rpm -ivh *.rpm
warning: postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 442df0f8
Preparing...                ########################################### [100%]
:postgresql94-libs      ########################################### [ 25%]
:postgresql94           ########################################### [ 50%]
:postgresql94-contrib   ########################################### [ 75%]
:postgresql94-server    ########################################### [100%]

建立PostgreSQL 預設庫


[root@web103 pg]# service postgresql-9.4 initdb
Initializing database: [  OK  ]
[root@web103 data]# pwd
/var/lib/pgsql/9.4/data
[root@web103 data]# ls -ltr
total 120
-rw------- 1 postgres postgres 21265 Jun 16 20:52 postgresql.conf
-rw------- 1 postgres postgres    88 Jun 16 20:52 postgresql.auto.conf
drwx------ 3 postgres postgres  4096 Jun 16 20:52 pg_xlog
-rw------- 1 postgres postgres     4 Jun 16 20:52 PG_VERSION
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_twophase
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_tblspc
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_subtrans
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_snapshots
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_serial
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_replslot
drwx------ 4 postgres postgres  4096 Jun 16 20:52 pg_multixact
drwx------ 4 postgres postgres  4096 Jun 16 20:52 pg_logical
-rw------- 1 postgres postgres  1636 Jun 16 20:52 pg_ident.conf
-rw------- 1 postgres postgres  4224 Jun 16 20:52 pg_hba.conf
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_dynshmem
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_clog
drwx------ 5 postgres postgres  4096 Jun 16 20:52 base
drwx------ 2 postgres postgres  4096 Jun 16 21:16 pg_log
drwx------ 2 postgres postgres  4096 Jun 16 21:16 global
-rw------- 1 postgres postgres    80 Jun 16 21:39 postmaster.pid
-rw------- 1 postgres postgres    59 Jun 16 21:39 postmaster.opts
drwx------ 2 postgres postgres  4096 Jun 16 21:39 pg_stat
drwx------ 2 postgres postgres  4096 Jun 16 21:39 pg_notify
drwx------ 2 postgres postgres  4096 Jun 16 22:00 pg_stat_tmp

另外還可以通過如下兩種方式建立


initdb -D /var/lib/pgsql/9.4/data
pg_ctl -D /var/lib/pgsql/9.4/data

設定PostgreSQL 開機自動啟動


[root@web103 pg]# chkconfig postgresql-9.4 on
[root@web103 pg]# chkconfig --list|grep post
postgresql-9.4  0:off   1:off   2:on    3:on    4:on    5:on    6:off

查看預設建立PostgreSQL 使用者


[root@web103 data]# more /etc/passwd|grep post
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

增加PATH環境變數


-bash-3.2$echo "PATH=$PATH:/usr/pgsql-9.4/bin;export PATH" >>~/.bash_profile

啟動PostgreSQL 資料庫


--方法1
-bash-3.2$ postgres -D /var/lib/pgsql/9.4/data
----或者
-bash-3.2$ postgres -D /var/lib/pgsql/9.4/data >~/pg.log 2>&1 &

方法2
pg_ctl start -l ~/pg.log -D /var/lib/pgsql/9.4/data

這裡如果在環境變數中配置了PGDATA,那-D也可以不指定,-l為指定日誌目錄,建議使用封裝的方法2啟動pg

查看PostgreSQL進程資訊


[root@web103 data]# ps -ef|grep post|grep -v grep
postgres  4432     1  0 21:39 ?        00:00:00 /usr/pgsql-9.4/bin/postgres -D /var/lib/pgsql/9.4/data
postgres  4433  4432  0 21:39 ?        00:00:00 postgres: logger process       
postgres  4435  4432  0 21:39 ?        00:00:00 postgres: checkpointer process   
postgres  4436  4432  0 21:39 ?        00:00:00 postgres: writer process          
postgres  4437  4432  0 21:39 ?        00:00:00 postgres: wal writer process     
postgres  4438  4432  0 21:39 ?        00:00:00 postgres: autovacuum launcher process  
postgres  4439  4432  0 21:39 ?        00:00:00 postgres: stats collector process

這裡可以看到pg也和oracle有幾分類似,有日誌進程,checkpoint進程,有寫進程等(具體以後分析)

停止PostgreSQL資料庫


-bash-3.2$ pg_ctl stop -D /var/lib/pgsql/9.4/data
waiting for server to shut down.... done
server stopped
-bash-3.2$  ps -ef|grep post|grep -v grep
root      6036   499  0 22:07 pts/0    00:00:00 su - postgres
postgres  6037  6036  0 22:07 pts/0    00:00:00 -bash
postgres  6113  6037  0 22:08 pts/0    00:00:00 ps -ef

PostgreSQL預設監聽連接埠

[root@web103 pgsql]# netstat -natp|grep postgres
tcp        0      0 127.0.0.1:5432              0.0.0.0:*                   LISTEN      4432/postgres




Linux下PostgreSQL 安裝問題解決全過程

PostgreSQL 的官方下載地址為:

  ftp://ftp.postgresql.org/pub/v7.1.3/postgresql-7.1.3.tar.gz
  http://www.postgresql.org/

  如果下載最新的開發版本,你需要下載並安裝 flex(版本號碼大於 2.5.4) 以及 bison (版本號碼大於 1.28)

  設計人員為了安全考慮,PostgreSQL 不能以 root 使用者運行,所以必須建立對應的使用者和組。

  # useradd postgre (自動建立 postgre 組)
        {註:在用useradd 命令添加一個使用者後,此沒有輸入密碼,系統並不會當空密碼繼續,而是無論什麼密碼都無法使用這個帳號,一定要用root通過passwd username給他設定一個密碼啟用此使用者後才可以使用;第二是在useradd的時候,通過-p參數,即useradd [username] -p [password]即可直接分配密碼並啟用使用者。}

  安裝的過程並不複雜和其他源碼版本的安裝方法類似:
  解壓到 /usr/local/src:
  # tar xvfz postgresql-7.1.3.tar.gz
  # cd postgresql-7.1.3
  # ./configure --prefix=/usr/local/pgsql
  # make
  # make install
  # chown -R postgre.postgre /usr/local/pgsql

  這樣安裝完畢後,並不是萬事大吉了,還有一些收尾工作要做:
  # vi ~postgre/.bash_profile
  添加:

  PGLIB=/usr/local/pgsql/lib
  PGDATA=$HOME/data
  PATH=$PATH:/usr/local/pgsql/bin
  MANPATH=$MANPATH:/usr/local/pgsql/man
        LD_LIBRARYPATH=$LD_LIBRARYPATH:/usr/local/pgsql/lib
  export PGLIB PGDATA PATH MANPATH LD_LIBRARYPATH

        修改配置
       <一>.
        $vi /home/postgre/data/pg_hba.conf
        在檔案的最底部:
        # "local" is for Unix domain socket   connections only
        local   all         all                               trust
        # IPv4 local connections:
        host    all         all         192.168.18.0/32 (*設定此處的網關)      trust
        # IPv6 local connections:
        host    all         all         ::1/128               trust
        <二>.
       $vi /home/postgre/data/postgresql.conf
       # - Connection Settings -
       # listen_addresses = 'localhost'            # what IP address(es) to listen on;
      把以上這一句之前的#刪除,並賦值為'*'
      即改為: listen_addresses='*'
 
      
  以上配置完後,以 postgres 使用者登入,
  # su - postgre
  建立資料庫目錄:
  $ mkdir data

  啟動資料庫引擎:

  $ initdb
  [postgre@www postgre]$ initdb
  This database system will be initialized with username "postgre".
  This user will own all the data files and must also own the server process.

  Fixing permissions on pre-existing data directory /home/postgre/data
  Creating database system directory /home/postgre/data/base
  Creating database XLOG directory /home/postgre/data/pg_xlog
  Creating template database in /home/postgre/data/base/template1
  Creating global relations in /home/postgre/data/base
  Adding template1 database to pg_database

  Creating view pg_user.
  Creating view pg_rules.
  Creating view pg_views.
  Creating view pg_tables.
  Creating view pg_indexes.
  Loading pg_description.
  Vacuuming database.

  Success. You can now start the database server using:

  /usr/local/pgsql/bin/postmaster -D /home/postgre/data
  or
  /usr/local/pgsql/bin/pg_ctl -D /home/postgre/data start

       以後台方式啟動postgre資料庫服務
  $ postmaster -i -D ~/data &
  [1] 22603
  [postgre@www postgre]$ DEBUG: Data Base System is starting up at Thu Jan 31 02:00:44 2002
  DEBUG: Data Base System was shut down at Thu Jan 31 01:57:58 2002
  DEBUG: Data Base System is in production state at Thu Jan 31 02:00:44 2002

  這樣 PostgreSQL 使用位於 /usr/local/pgsql/data 的資料庫,允許 Internet 使用者的串連( -i ) ,並在後台運行。

      建立一個資料庫
看看你能否訪問資料庫伺服器的第一個例子就是試著建立一個資料庫。 一台運行著的 PostgreSQL 伺服器可以管理許多資料庫。 通常我們會為每個項目和每個使用者單獨使用一個資料庫。

你的節點管理員可能已經為你建立了可以使用的資料庫。 他應該已經告訴你這個資料庫的名字。如果這樣你就可以省略這一步, 並且跳到下一節。

要建立一個新的資料庫,在我們這個例子裡叫 mydb,你可以使用下面的命令:

$ createdb mydb它應該產生下面這樣的響應:
{註:若出現bash: createdb: command not found,此時可能是你未將pgsql
         的bin加到PATH環境變數中,此時在shell下執行如
          $export PATH=$PATH:/usr/local/pgsql/bin即可,同理以後若命令不存
      在的時候也可以使用此方法試一試,看一下bin的目錄是否加到PATH.
}

CREATE DATABASE如果這樣,那麼這一步就成功了,你就可以忽略本節餘下的部分了。

如果你看到類似下面這樣的資訊

createdb: command not found那麼就是PostgreSQL沒有安裝好。要麼是就根本沒裝上, 要麼是你的搜尋路徑沒有設定正確。嘗試用絕對路徑調用該命令試試:

$ /usr/local/pgsql/bin/createdb mydb在你的節點上這個路徑可能不一樣。和你的管理員聯絡或者看看安裝指導擷取正確的位置。

另外一種響應可能是這樣:

createdb: could not connect to database postgres: could not connect to server:
No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?這意味著該伺服器沒有啟動,或者沒有在 createdb 預期的地方啟動。同樣, 你也要檢查安裝指導或者找管理員。

另外一個響應可能是這樣:

createdb: could not connect to database postgres: FATAL:  user "joe" does not
exist在這裡提到了你自己的登陸名。如果管理員沒有為你建立 PostgreSQL 使用者帳號, 就會發生這些現象。(PostgreSQL 使用者帳號和作業系統使用者帳號是不同的。) 如果你是管理員,參閱 Chapter 18 擷取建立使用者帳號的協助。 你需要變成安裝 PostgreSQL 的作業系統使用者的身份(通常是 postgres)才能建立第一個使用者帳號。 也有可能是賦予你的 PostgreSQL 使用者名稱和你的作業系統使用者名稱不同; 這種情況下,你需要使用 -U 開關或者使用 PGUSER 環境變數聲明你的 PostgreSQL 使用者名稱。

如果你有個資料庫使用者帳號,但是沒有建立資料庫所需要的許可權,那麼你會看到下面的東西:

createdb: database creation failed: ERROR:  permission denied to create database並非所有使用者都經過了建立新資料庫的授權。 如果 PostgreSQL 拒絕為你建立資料庫, 那麼你需要讓節點管理員賦予你建立資料庫的許可權。出現這種情況時請諮詢你的節點管理員。 如果你自己安裝了 PostgreSQL, 那麼你應該以你啟動資料庫伺服器的使用者身份登陸然後參考手冊完成許可權的賦予工作。 [1]

你還可以用其它名字建立資料庫。 PostgreSQL 允許你在一個節點上建立任意數量的資料庫。 資料庫名必須是以字母開頭並且小於 63 個字元長。 一個方便的做法是建立和你目前使用者名同名的資料庫。 許多工具假設該資料庫名為預設資料庫名,所以這樣可以節省你的敲鍵。 要建立這樣的資料庫,只需要鍵入

$ createdb
如果你再也不想使用你的資料庫了,那麼你可以刪除它。 比如,如果你是資料庫 mydb 的所有人(建立人), 那麼你就可以用下面的命令刪除它:

$ dropdb mydb(對於這條命令而言,資料庫名不是預設的使用者名稱。這樣你就必須聲明它。) 這個動作物理上將所有與該資料庫相關的檔案都刪除並且不可取消, 因此做這件事之前一定要想清楚。

Notes
[1] 為什麼這麼幹就行了? 解釋:PostgreSQL 使用者名稱是和作業系統使用者帳號分開的。 如果你與一個資料庫聯結,你可以選擇以何種 PostgreSQL 使用者名稱進行聯結; 如果你不選擇,那麼預設就是你的當前作業系統帳號。 如果這樣,那麼總有一個與作業系統使用者同名的 PostgreSQL 使用者帳號用於啟動伺服器, 並且通常這個使用者都有建立資料庫的許可權。如果你不想以該使用者身份登陸, 那麼你也可以在任何地方聲明一個 -U 選項以選擇一個聯結的 PostgreSQL 使用者名稱。

訪問資料庫
一旦你建立了資料庫,你就可以訪問它:



運行 PostgreSQL 互動的終端程式, 叫 psql, 它允許你互動地輸入,編輯,和執行 SQL 命令。

使用我們現有的圖形前端工具,比如 PgAccess 或者帶 ODBC 支援的辦公套件來建立和管理資料庫。 這種方法在這份教程中沒有介紹。

寫一個客戶應用,使用多種語言綁定中的一種。 這些可能性在 Part IV 中有更深入的討論。

你可能需要啟動 psql,實驗本教程中的例子。 你可以用下面的命令為 mydb 資料庫啟用它:

$ psql mydb如果你省略了資料庫名字,那麼它預設就是你的使用者帳號名字。 你已經在前面的小節裡知道這個大綱了。

在 psql 裡,你會看到下面的歡迎資訊:

Welcome to psql 8.1, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

mydb=>最後一行也可能是

mydb=#這個提示符意味著你是資料庫超級使用者,最可能出現在你自己安裝了 PostgreSQL 的情況下。作為超級使用者意味著你不受存取控制的限制。 對於本教程的目的而言,是否超級使用者並不重要。

如果你啟動 psql 時碰到了問題,那麼回到前面的小節。 診斷 createdb 的方法和診斷 psql 的方法很類似, 如果前者能運行那麼後者也應該能運行。

psql 列印出的最後一行是提示符,它表示 psql 正聽著你說話,這個時候你就可以敲入 SQL 查詢到一個 psql 維護的工作區中。實驗一下下面的命令:

mydb=> SELECT version();
                            version
----------------------------------------------------------------
PostgreSQL 8.1 on i586-pc-linux-gnu, compiled by GCC 2.96
(1 row)
mydb=> SELECT current_date;
    date
------------
2002-08-31
(1 row)

mydb=> SELECT 2 + 2;
?column?
----------
        4
(1 row)
psql 程式有一些不屬於 SQL 命令的內部命令。 它們以反斜線開頭,"\"。 有些這種命令在歡迎資訊中列出。比如,你可以用下面的命令擷取各種PostgreSQL SQL 命令的協助文法:

mydb=> \h
要退出 psql,鍵入

mydb=> \q然後 psql 就會退出並且給你返回到命令列 shell。 (要擷取更多有關內部命令的資訊,你可以在 psql 提示符上鍵入 \?。) psql 的完整功能在 Part VI 中有文檔。如果 PostgreSQL 安裝正確,那麼你還可以在作業系統的 shell 提示符上鍵入 man psql 來閱讀該文檔。在這份文檔裡,我們將不會明確使用這些特性,但是你自己可以在合適的時候使用它們。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.