在 mac 下,能夠利用 homebrew 直接安裝 PostgreSQL:php
1
|
brew
install
postgresql -
v
|
稍等片刻,PostgreSQL 就安裝完成。接下來就是初始數據庫,在終端執行一下命令,初始配置 PostgreSQL:html
1
|
initdb
/usr/local/var/postgres
-E utf8
|
上面指定 "/usr/local/var/postgres" 爲 PostgreSQL 的配置數據存放目錄,而且設置數據庫數據編碼是 utf8,更多配置信息能夠 "initdb --help" 查看。sql
設成開機啓動 PostgreSQL:shell
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:app
1
|
pg_ctl -D
/usr/local/var/postgres
stop -s -m fast
|
建立一個 PostgreSQL 用戶post
1
2
3
|
createuser username -P
#Enter password for new role:
#Enter it again:
|
上面的 username 是用戶名,回車輸入 2 次用戶密碼後即用戶建立完成。更多用戶建立信息能夠 "createuser --help" 查看。fetch
建立數據庫this
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
|