星期五, 七月 13, 2007

Debian Linux 架設 PostgreSQL + PhpPgAdmin

紀錄在 Debian Linux 上安裝 PostgreSQL 和 phppgadmin 的設定和過程~ :)

安裝 postgresql 

  • apt-get install postgresql # 照理說這樣子 Postgre SQL 就已經裝好了.

測試看看 PosrgreSQL 基本操作:

  1. sudo -u postgres psql template1 # 要用 postgres 的身份執行才可以
  2. template1=# create user PG_USER with password 'PASSWORD' createdb createuser; # 建立新帳號/密碼
  3. template1=# alter user postgres with password 'PASSWORD'; # 修改postgres的密碼
  4. template1=# \q # 離開
  5. PS: 於此處下的指令, 都會存在 /var/lib/postgresql/.psql_history 這個檔案裡.

再來來架設 phppgadmin:

  1. 安裝 phppgadmin
  2. apt-get install phppgadmin libapache2-mod-php5 
  3. 將 /phppgadmin 指到 phppgadmin 預設安裝路徑(/usr/share/phppgadmin), 採用 package 寫好的 conf 檔
  4. ln -s /etc/phppgadmin/apache.conf /etc/apache2/sites-enabled/001-phppgadmin
  5. 修改權限設定, 預設只讓本機自己存取, 改成都先不限.
  6. vim /etc/phppgadmin/apache.conf
  7. 將下述
    allow from 127.0.0.0/255.0.0.0
    # allow from all
  8. 改成
    #allow from 127.0.0.0/255.0.0.0
    allow from all
  9. /etc/init.d/apache2 reload
  10. 這樣子就完工囉!!

phppgadmin 預設是不讓 postgres 登入, 所以最好要先 create 一個 user 再登入使用.

  1. 如果想要讓 postgres 登入, 一定要先修改 postgres 的密碼(postgres 的密碼不能為空)
  2. vim /etc/phppgadmin/config.inc.php
  3. 修改 $conf['extra_login_security'] = true;
  4. 變成 $conf['extra_login_security'] = false;
  5. 這樣子就可以用 postgres 登入.

 預設 PostgreSQL data 儲存路徑: /var/lib/postgresql/8.2/main, 修改成自定的路徑:

  1. mkdir -p /var/postgresql/data
  2. chown -R postgres:postgres /var/postgresql/
  3. sudo su - postgres # 轉換成 postgres 的身份
  4. vim ~/.bashrc # 設定環境參數, 將下述寫入 .bashrc 檔最下面即可
  5. export POSTGRE_HOME=/usr/lib/postgresql/8.2
  6. export PGLIB=$POSTGRE_HOME/lib
  7. export PGDATA=/var/postgresql/data
  8. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"
  9. export PATH=$PATH:$POSTGRE_HOME/bin
  10. source ~/.bashrc
  11. initdb --pgdata=/var/postgresql/data
  12. 啟動 DB:(下述二者挑其一即可)
  13. pg_ctl -D /var/postgresql/data -l logfile start
  14. postgres -D /var/postgresql/data

參考:

相關標籤

this is comment icon Postgresql 的 錯誤訊息 [回覆]

如果在安裝完postgresql-8.1
他正在啟動的時候出現下列訊息
* Restarting PostgreSQL 8.1 database server * The PostgreSQL server failed to start. Please check the log output:
2007-07-30 11:37:41 CST LOG: 無法載入根憑證檔"root.crt":No SSL error reported
2007-07-30 11:37:41 CST 詳細:Will not verify client certificates.
2007-07-30 11:37:41 CST LOG: could not translate host name "localhost", service "5432" to address: Name or service not known
2007-07-30 11:37:41 CST WARNING: 無法為"localhost"建立接受連線的socket
2007-07-30 11:37:41 CST 嚴重錯誤: could not create any TCP/IP sockets
[fail]

他的意思好像是root的憑證有問題,但是我不知道為什麼會這樣,但是我用另外一台安裝似乎沒有這樣的問題耶。

2台系統都是
Ubuntu Server 7.04
Apache2
Mysql
php5

Comment by 葉子 (07/30/2007 12:06)

this is comment icon 回 葉子 [回覆]

我猜您另一台是設到 ssl 了, 但是我是沒試過, 我做的話應該會從 postgresql.conf 去查查看有哪邊不同吧.

您可以參考下面兩篇看看:
http://www.postgresql.org/docs/8.2/static/ssl-tcp.html
http://www.postgresql.org/docs/8.2/interactive/libpq-ssl.html

Comment by Tsung (07/30/2007 14:47)

this is comment icon [回覆]

感謝>"<

似乎是我的DomainName的關係。
因為那台暫時還沒有DomainName,不過我的憑證common name設定localhost應該也是可以才對。

我用openssl改了半天,還是打不開

後來我把/var/lib/postgresql/8.2/main/ 整個砍了。SQL就打開了 @_@
不過這樣好像不太對......

Comment by 葉子 (07/31/2007 01:51)

this is comment icon 回 葉子 [回覆]

哈~ :)
能解決問題就是好解法, 沒什麼特別對錯的 :P

Comment by Tsung (07/31/2007 10:58)

this is comment icon phpgpadmin 登入問題 [回覆]

你好..

我完全跟著 Tsung's Blog 的教學做了一次,
然後開了 http://localhost/phppgadmin/
但 登入的時候
登入是不是 這個呢 ?
Username: PG_USER
Password: PASSWORD

因為我怎樣登入也是 Login failed[/code]

Comment by KitshiNg (09/12/2007 21:18)

this is comment icon 回 KitshiNg [回覆]

厄, 我上面全部大寫的東西, 事實上是當變數, 應該說可以換成自己想取的名字. XD

您或許可以試試 pg_user 小寫的 login 看看.

不然就是先看第 10 步驟下面的, 試試用 postgres 登入看看.
再進去看 pg_user 的這個帳號有沒有被開啟了~ 試試看吧 :)

Comment by Tsung (09/12/2007 21:36)

this is comment icon 另一問題 [回覆]

其實我不明白,下面你意思是甚麼 ?

『將 /phppgadmin 指到 phppgadmin 預設安裝路徑(/usr/share/phppgadmin), 採用 package 寫好的 conf 檔』

/phppgadmin ??? (我太新了,請詳細一點@@")

Comment by KitshiNg (09/12/2007 22:08)

this is comment icon 還有的問題" [回覆]

我每一次 restart service 時都有一句 command not found ,
但結果又說是 postmaster successfully started
為甚麼呢 ? ( 我雖然是跟著你的教學去做,但我安裝的是 postgreSQL 7.4

postgres@debianserver:~$ pg_ctl -D /var/postgresql/data/ restart
pg_ctl: could not find /var/postgresql/data//postmaster.pid
Is postmaster running?
starting postmaster anyway
cat: /var/postgresql/data//postmaster.opts: ���������������������������
/usr/lib/postgresql/7.4/bin/pg_ctl: line 359: : command not found
postmaster successfully started

Comment by KitshiNg (09/12/2007 22:26)

this is comment icon 用 phppgadmin 以 postgres login 時,Login failed [回覆]

已經跟著上面修改了
vim /etc/phppgadmin/config.inc.php

然後...

debianserver:~# sudo -u postgres psql template1
Welcome to psql 7.4.17, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
template1=# alter user postgres with password '123456';
ALTER USER
template1=# \q
debianserver:~# source /etc/postgresql

在phppgadmin 登入時 ===> Login failed

Comment by KitshiNg (09/12/2007 22:35)

this is comment icon 回 KitshiNg [回覆]

嗯嗯, 不好意思, 是我寫不好.. Orz..

『將 /phppgadmin 指到 phppgadmin 預設安裝路徑(/usr/share/phppgadmin), 採用 package 寫好的 conf 檔』 => 這意思是指第 4 點的 "ln -s ..." 做的事情是這句話.

然後你要不要安裝看看 8.0, 因為我記得 7.4 有些小問題要改(我忘記之前有改什麼了).

你的看起來是找另找不到 pg_ctl, 然後啟動好像根本沒有起來.(因為啟動完應該會有 pid, 但是你的 pid file 是找不到的.)

Comment by Tsung (09/12/2007 22:36)

this is comment icon 回 KitshiNg [回覆]

改完密碼後, 試著下面二選一, 重新啟動 postgresql DB 看看.(路徑寫你 DB 存的路徑)

# pg_ctl -D /var/postgresql/data -l logfile start
# postgres -D /var/postgresql/data

Comment by Tsung (09/12/2007 22:45)

this is comment icon 好吧!! 我立即安裝 8.1 [回覆]

好吧~

我現在 apt-get remove postgresql

然後 apt-get install postgresql-8.1

我再做回以上的步驟看看有沒有問題吧...Thanks

p.s. 我忘了說我是用 Debian 4.0 的

Comment by KitshiNg (09/12/2007 22:51)

this is comment icon 回 KitshiNg [回覆]

嗯~ 我想應該 Debian 4.0 這些都沒有關係的.
(但是 PostgreSQL 7.4 是有點關係, 因為 7.4 的 deb package 好像包的不太好, 有不少地方要改就是了 :()

Comment by Tsung (09/12/2007 22:57)

this is comment icon 下面還是用 Postgresql 7.4 的 [回覆]

下面還是用 Postgresql 7.4 的

postgres@debianserver:~$ pg_ctl -D /var/postgresql/data/ -l logfile1 start
postmaster successfully started
postgres@debianserver:~$ postgres -D /var/postgresql/data/
FATAL: database "postgres" does not exist
postgres@debianserver:~$ vim logfile1
下面是 logfile 內容
FATAL: lock file "/var/run/postgresql/.s.PGSQL.5432.lock" already exists
HINT: Is another postmaster (PID 2092) using socket file "/var/run/postgresql/.s.PGSQL.5432"?

總是問題多多 >.<

Comment by KitshiNg (09/12/2007 22:58)

this is comment icon 回 KitshiNg [回覆]

Sorry 應該要 sudo 或 sudo -u postgres 再執行命令... Orz...

Comment by Tsung (09/12/2007 23:20)

this is comment icon 算是成功了@@" [回覆]

我再 apt-get install postgresql-8.1

重新所有程序

成功用自己建立的 pg_user 登入了 :)

但我打 pg_ctl -D /var/postgresql/data -l logfile start

再查看 vim logfile

看見

LOG:
������bind
IPv4
sockett��������������������

������Is
another postmaster already running on port 5432? If
not, wait a few seconds and retry.
WARNING:
���������"localhost""�������・���������socket

�������:
could not create any TCP/IP sockets

為什麼比 7.4 的 logfile 還要多問號 呢 ?
而且大概出了甚麼問題?

- -- - -- - - - - - - - - - - - - - - - - - - - --
『Sorry 應該要 sudo 或 sudo -u postgres 再執行命令... Orz...』

Kitshing: 只是我太新,一定概念也沒有,不知道要用 sudo ^^"
- - - - - -- -- - - - - --- - - - - - - - - - - - - - -- -

Comment by KitshiNg (09/12/2007 23:45)

this is comment icon 回 KitshiNg [回覆]

因為上面寫 "already running", 我猜應該是這個原因吧~
您可以試試看先停掉再開, 應該就不會有那麼多 error 了, 恭喜您成功囉 :D

Comment by Tsung (09/12/2007 23:51)

this is comment icon [回覆]

另外,請問為什麼這個 logfile error 有這麼多問號呢 ?
如何可以令出的 error 變會文字 ?

====

停了再重新啓動也是有 error

但用 restart 就沒問題了

真的十分謝謝 :)

( 我被這個 7.4 玩了一個星期了 -_-" )

Comment by KitshiNg (09/13/2007 00:00)

this is comment icon 回 KitshiNg [回覆]

我猜那些是中文字, 或許設定 postgres 的 .bashrc, LANG=en 看看.

停了再重新啟動也有此問題, 應該是沒停乾淨, 你試試停了後, 再 netstat -tln | grep 5432 或 lsof -Pni |grep ":5432" 看看.
等到都沒有秀出值後再啟動.

因為錯誤訊息寫說還有東西在跑:
another postmaster already running on port 5432? If
not, wait a few seconds and retry

辛苦了, 我記得之前搞 7.4 發現問題還不少. Orz..

Comment by Tsung (09/13/2007 09:17)

this is comment icon 看來停不了服務@_@ [回覆]

postgres@debianserver:~$ netstat -tln | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
postgres@debianserver:~$ pg_ctl -D /var/postgresql/data/ -l logfile stop
pg_ctl���PID���"/var/postgresql/data/postmaster.pid"���������
postmaster���������������������
postgres@debianserver:~$ pg_ctl -D /var/postgresql/data/ -l logfile start
postmaster������������
postgres@debianserver:~$ pg_ctl -D /var/postgresql/data/ -l logfile stop
pg_ctl���PID���"/var/postgresql/data/postmaster.pid"���������
postmaster���������������������
postgres@debianserver:~$ netstat -tln | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
postgres@debianserver:~$

用到了,停不了..@_@

Comment by KitshiNg (09/14/2007 00:00)
Add this page to del.icio.us

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~