在 mac 下,可以利用 homebrew 直接安裝 PostgreSQL:
1
|
brew
install
postgresql -
v
|
稍等片刻,PostgreSQL 就安裝完成。接下來就是初始數據庫,在終端執行一下命令,初始配置 PostgreSQL:
1
|
initdb
/usr/local/var/postgres
-E utf8
|
上面指定 "/usr/local/var/postgres" 為 PostgreSQL 的配置數據存放目錄,並且設置數據庫數據編碼是 utf8,更多配置信息可以 "initdb --help" 查看。
設成開機啟動 PostgreSQL:
1
2
|
ln
-sfv
/usr/local/opt/postgresql/
*.plist ~
/Library/LaunchAgents
launchctl load ~
/Library/LaunchAgents/homebrew
.mxcl.postgresql.plist
|
啟動 PostgreSQL:
1
|
pg_ctl -D
/usr/local/var/postgres
-l
/usr/local/var/postgres/server
.log start
|
關閉 PostgreSQL:
1
|
pg_ctl -D
/usr/local/var/postgres
stop -s -m fast
|
創建一個 PostgreSQL 用戶
1
2
3
|
createuser username -P
#Enter password for new role:
#Enter it again:
|
上面的 username 是用戶名,回車輸入 2 次用戶密碼后即用戶創建完成。更多用戶創建信息可以 "createuser --help" 查看。
創建數據庫
1
|
createdb dbname -O username -E UTF8 -e
|
上面創建了一個名為 dbname 的數據庫,並指定 username 為改數據庫的擁有者(owner),數據庫的編碼(encoding)是 UTF8,參數 "-e" 是指把數據庫執行操作的命令顯示出來。
更多數據庫創建信息可以 "createdb --help" 查看。
連接數據庫
1
|
psql -U username -d dbname -h 127.0.0.1
|
PostgreSQL 數據庫操作
顯示已創建的數據庫:
1
|
\l
|
在不連接進 PostgreSQL 數據庫的情況下,也可以在終端上查看顯示已創建的列表:
1
|
psql -l
|
連接數據庫
1
|
\c dbname
|
顯示數據庫表
1
|
\d
|
創建一個名為 test 的表
1
|
CREATE
TABLE
test(id
int
, text
VARCHAR
(50));
|
插入一條記錄
1
|
INSERT
INTO
test(id, text)
VALUES
(1,
'sdfsfsfsdfsdfdf'
);
|
查詢記錄
1
|
SELECT
*
FROM
test
WHERE
id = 1;
|
更新記錄
1
|
UPDATE
test
SET
text =
'aaaaaaaaaaaaa'
WHERE
id = 1;
|
刪除指定的記錄
1
|
DELETE
FROM
test
WHERE
id = 1;
|
刪除表
1
|
DROP
TABLE
test;
|
刪除數據庫
1
|
DROP
DATABASE
dbname;
|
或者利用 dropdb 指令,在終端上刪除數據庫
1
|
dropdb -U
user
dbname
|
下面是自用的 PostgreSQL 的 php 操作類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<?php
define(
"HOST"
,
"127.0.0.1"
);
define(
"PORT"
, 5432);
define(
"DBNAME"
,
"dbname"
);
define(
"USER"
,
"user"
);
define(
"PASSWORD"
,
"password"
);
class
Ext_Pgsql {
//單例
private
static
$instance
= null;
private
$conn
= null;
private
function
__construct()
{
$this
->conn = pg_connect(
"host="
. HOST .
" port="
. PORT .
" dbname="
. DBNAME .
" user="
. USER .
" password="
. PASSWORD)
or
die
(
'Connect Failed : '
. pg_last_error());
}
public
function
__destruct()
{
@pg_close(
$this
->conn);
}
/**
* 單例模式
* @param $name
*/
public
static
function
getInstance()
{
if
( ! self::
$instance
)
{
self::
$instance
=
new
self();
}
return
self::
$instance
;
}
/**
* 獲取記錄
*/
public
function
fetchRow(
$sql
)
{
$ret
=
array
();
$rs
= pg_query(
$this
->conn,
$sql
);
$ret
= pg_fetch_all(
$rs
);
if
( !
is_array
(
$ret
) )
{
return
array
();
}
return
$ret
;
}
/**
* 執行指令
* @param string $sql
*/
public
function
query(
$sql
)
{
$result
= pg_query(
$this
->conn,
$sql
);
if
( !
$result
)
die
(
"SQL : {$sql} "
. pg_last_error());
}
/**
* 獲取一條記錄
*/
public
function
fetchOne(
$sql
)
{
$ret
=
array
();
$rs
= pg_query(
$this
->conn,
$sql
);
$ret
= pg_fetch_all(
$rs
);
if
( !
is_array
(
$ret
) )
{
return
array
();
}
return
current(
$ret
);
}
}
?>
|
一些問題
PostgreSQL 9.2 版本升級到 9.3.1 版本后的數據兼容問題
連接 PostgreSQL 時報以下錯誤:
1
2
3
|
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
|
打開 PostgreSQL 的服務日志發現是 PostgreSQL 9.2 版本升級到 9.3.1 版本后的數據兼容問題:
1
2
3
|
tail -f /usr/local/var/postgres/server.log
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.1.
|
對於版本的數據升級問題,PostgreSQL 提供了 pg_upgrade 來做版本后的數據遷移,用法如下:
1
|
pg_upgrade -b 舊版本的bin目錄 -B 新版本的bin目錄 -d 舊版本的數據目錄 -D 新版本的數據目錄 [其他選項...]
|
數據遷移前,記得先關閉 PostgreSQL 的 postmaster 服務,不然會報以下錯誤:
1
2
3
|
There seems to be a postmaster servicing the new cluster.
Please shutdown that postmaster and try again.
Failure, exiting
|
利用 pg_ctl 關閉 postmaster:
1
|
pg_ctl -D /usr/local/var/postgres stop
|
Mac 下也可以這樣關閉:
1
|
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
|
首先備份就版本的數據(默認是在 /usr/local/var/postgres 目錄):
1
|
mv /usr/local/var/postgres /usr/local/var/postgres.old
|
利用 initdb 命令再初始一個數據庫文件:
1
|
initdb /usr/local/var/postgres -E utf8 --locale=zh_CN.UTF-8
|
NOTE:記得加 "--locale=zh_CN.UTF-8" 選項,不然會報以下錯誤:
1
|
lc_collate cluster values do not match: old "zh_CN.UTF-8", new "en_US.UTF-8"
|
最后運行 pg_upgrade 進行數據遷移:
1
|
pg_upgrade -b /usr/local/Cellar/postgresql/9.2.4/bin/ -B /usr/local
|