顯示具有 Ubuntu 標籤的文章。 顯示所有文章
顯示具有 Ubuntu 標籤的文章。 顯示所有文章

2013年1月21日 星期一

[Ubuntu] 安裝PostgreSQL9.1步驟

雖然一開始Ubuntu就安裝了postgresql,但是在搞不清楚狀況的一開始我還是把它給砍了= =
然後再重裝一遍:
sudo apt-get install -y postgresql-9.1 postgresql-client-9.1 postgresql-contrib-9.1

相關的執行檔會被放在這個資料夾:
/usr/lib/postgresql/9.1/bin

在安裝時一直遇到找不到放置PGDATA的錯誤訊息,所以我們要在9.1資料夾下創建一個"data"資料夾,並更改擁有者為postgres:
mkdir /usr/lib/postgresql/9.1/data
chown postgres /usr/lib/postgresql/9.1/data
此時再執行su postgres,切換為postgres身分,在/usr/lib/postgresql/9.1/bin的路徑下執行:
./initdb -D mkdir ../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 en_US.UTF-8.
The default database encoding has accordingly been set to UTF8.
The default text search configuration will be set to "english".

fixing permissions on existing directory ../data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 24MB
creating configuration files ... ok
creating template1 database in ../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 -A option the
next time you run initdb.

Success. You can now start the database server using:

    ./postgres -D ../data
or
    ./pg_ctl -D ../data -l logfile start


接著我們就可以執行
./postgres -D ../data

./pg_ctl -D ../data -l logfile start
來啟動服務了。

Postgresql這時候已經可以接受連線,我們來執行createdb指令來新增一個資料庫:
postgres@li440-29:/usr/lib/postgresql/9.1/bin$ createdb test

然後執行psql -d test來進入console,如果直接執行psql是會直接進入預設的postgres資料庫的,快樂的打指令之前請先確認一下所在的DB。

而pg_ctl是用來對postgres做操作的控制台,可以執行"pg_ctl --help"來查看它的功能,這邊先列出一部分:
Usage:
  pg_ctl init[db]               [-D DATADIR] [-s] [-o "OPTIONS"]
  pg_ctl start   [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"]
  pg_ctl stop    [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
  pg_ctl restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
                 [-o "OPTIONS"]
  pg_ctl reload  [-D DATADIR] [-s]
  pg_ctl status  [-D DATADIR]
  pg_ctl promote [-D DATADIR] [-s]
  pg_ctl kill    SIGNALNAME PID

此時執行netstat -ap應該能看到postgres正在listen

但因為每次啟動PG都要輸入-D參數實在太麻煩,我們是先將PGDATA加入環境變數中,打開~/.bashrc,在最後一行加上:
export PGDATA=/usr/lib/postgresql/9.1/data

後記

在重啟服務時,常常會有失敗的情況,這時只要切換終止模式即可:
./pg_ctl -D ../data restart -m fast

2012年11月17日 星期六

[宅] 在Ubuntu安裝PostgreSQL

之前的環境是在windows,現在開始要慢慢搬到Ubuntu上,但是在安裝的過程中碰到了一點麻煩,在這邊做一個筆記,希望有相同問題的人或自己以後能更順利。

Install PostgreSQL 

安裝的過程很簡單沒有什麼問題,但是不知道為什麼9.1版在我的電腦跑不起來,所以改裝8.4
sudo apt-get install postgresql-8.4


啟動PostgreSQL

sudo /etc/init.d/postgresql-8.4 start
如果不行的話把版本去掉
sudo /etc/init.d/postgresql start

其他postgredsql操作:
sudo /etc/init.d/postgresql stop
sudo /etc/init.d/postgresql restart
sudo /etc/init.d/postgresql reload


設定database

對於postgresql的相關操作都是在psql這個postgresql的shell底下做的,但是一般使用者的權限不能執行他,就算你加了sudo也是一樣,所以這時候我們要切換到postgres在安裝過程中為我們建立的使用者"postgres":
sudo su postgres
接著我們就會看到shell的輸入欄位變成這樣:postgres@yourhost:
然後就可以開心的進入psql囉~
首先我們先來看看目前的使用者有誰,輸入"\du",會出現以下的畫面:

首先就是修改postgre這個superuser的密碼
ALTER USER postgres WITH PASSWORD ' <***password***> ';

要注意的是,在psql內下的SQL指令的結尾,要加上分號";"後才會執行,所以你也可以分成好幾行來打,最後一行加上分號再按ENTER就會執行了。
postgres=# SELECT *
postgres-# FROM BOOKS;


重要SQL指令

到了這邊大概都沒啥問題了,只剩一些Database和table的設定罷了。


  • 建立資料庫 CREATE DATABASE <dbname>
  • 建立資料表 CREATE TABLE <tablename>(COLUMN …)
  • 刪除資料庫 DROP DATABASE <dbname>
  • 刪除資料表 DROP TABLE <tablename>
  • 修改資料表 ALTER TABLE <tablename> …
  • 修改使用者 ALTER USER <user> WITH …


要查詢更細的設定只要在"\h"之後加上指令即可

2012年10月26日 星期五

[宅] 菜鳥出槌筆記 - 本地化設置錯誤

今天要執行mongo指令時,一直出現以下錯誤訊息:

terminate called after throwing an instance of 'std::runtime_error'

  what():  locale::facet::_S_create_c_locale name not valid

Aborted (core dumped)

上網查了一下發現是本地設定的問題,所以在mongo指令之前加上"LC_ALL=C"就可以成功執行了
LC_ALL=C mongo