PostgreSQL資料型別

前言

因為最近有碰PostgreSQL的需要,因此來整理儲存型別。
此外,發現PostgreSQL的欄位型態具有Alias別名,所以有些型別找不到就要參考文件的Alias。
在以下只列常見的型別,更多請自己參閱參考資料。

常見資料型別

字串

名稱 描述 備註
character varying(n), varchar(n) 可變長度,但有限制 其中 n 是正整數。可以長度最多為 n 個字元(不是位元組)的字串。
character(n), char(n) 固定長度,空白填充 其中 n 是正整數。可以長度最多為 n 個字元(不是位元組)的字串。
text 可變且無限長度

數字

名稱 描述 備註
smallint 2 bytes ,-32768 to +32767
integer 4 bytes,-2147483648 to +2147483647
bigint 8 bytes,-9223372036854775808 to +9223372036854775807
numeric up to 131072 digits before the decimal point; up to 16383 digits after the decimal point。可調式精確度數值型別 Alias decimal
real 4Bytes,6 decimal digits precision 非精度浮點數型別
double precision 8Bytes,15 decimal digits precision 非精度浮點數型別

布林

名稱 描述 備註
boolean 1Bytes

日期時間

名稱 描述 備註
timestamp [ (p) ] [ without time zone ] 8 bytes, both date and time (no time zone), 4713 BC ~ 294276 AD
timestamp [ (p) ] with time zone 8 bytes, both date and time, with time zone, 4713 BC ~ 294276 AD

總結

在這篇中以一個熟悉SQL Server的角度,列出了平常可能會見到的PostgreSQL資料型態。
因為後者的資料型別彈性高(能夠自己建立物件存進、支援儲存JSON等等),所以等後面實際碰到再額外補充。
而完整的型別請自行參考網址的參考資料吧!

可能會有人有疑問怎沒SQL Server的NVARCHAR?
Generally you just use a UTF-8 encoded DB, and everything works with no hassle.

參考資料