postgresql初始化之initdb的使用詳解
initdb創(chuàng)建了一個(gè)新的PostgreSQL數(shù)據(jù)庫(kù)集群。數(shù)據(jù)庫(kù)集群是由單個(gè)服務(wù)器實(shí)例管理的數(shù)據(jù)庫(kù)集合。
創(chuàng)建數(shù)據(jù)庫(kù)集群包括數(shù)據(jù)庫(kù)所在的目錄、生成共享目錄表(屬于整個(gè)集群而不是任何特定數(shù)據(jù)庫(kù)的表)以及創(chuàng)建template1和postgres數(shù)據(jù)庫(kù)。稍后創(chuàng)建新數(shù)據(jù)庫(kù)時(shí),將復(fù)制template1數(shù)據(jù)庫(kù)中的所有內(nèi)容(因此,template1中安裝的任何內(nèi)容都會(huì)自動(dòng)復(fù)制到以后創(chuàng)建的每個(gè)數(shù)據(jù)庫(kù)中。),postgres數(shù)據(jù)庫(kù)是供用戶、實(shí)例程序和第三方應(yīng)用程序使用的默認(rèn)數(shù)據(jù)庫(kù)。
盡管initdb將嘗試創(chuàng)建指定的數(shù)據(jù)目錄,但如果所需數(shù)據(jù)目錄的父目錄是根目錄,則它可能沒有權(quán)限。要再這樣的設(shè)置中初始化,需要?jiǎng)?chuàng)建一個(gè)空數(shù)據(jù)目錄作為根目錄,然后使用chown將目錄的所有權(quán)分配給數(shù)據(jù)庫(kù)用戶賬戶,然后su成為運(yùn)行initdb的數(shù)據(jù)庫(kù)用戶。
initdb必須作為擁有服務(wù)器進(jìn)程的用戶運(yùn)行,因?yàn)榉?wù)器需要訪問initdb創(chuàng)建的文件和目錄。由于服務(wù)器不能作為root用戶運(yùn)行,所以也不能作為root用戶運(yùn)行initdb。
出于安全原因,在默認(rèn)情況下,initdb創(chuàng)建的新集群只能由集群所有者訪問?!猘llow-group-access選項(xiàng)允許與集群所有者屬于同一組的任何用戶讀取集群中的文件。這對(duì)于作為非特權(quán)用戶執(zhí)行備份非常有用。
initdb初始化數(shù)據(jù)庫(kù)集群的默認(rèn)語(yǔ)言環(huán)境和字符集編碼。在創(chuàng)建數(shù)據(jù)庫(kù)時(shí),可以分別設(shè)置字符集編碼、排序順序(LC_COLLATE)和字符集類(LC_CTYPE,例如upper、lower、digit)。initdb確定template1數(shù)據(jù)庫(kù)的那些設(shè)置,這些設(shè)置將作為所有其他數(shù)據(jù)庫(kù)的默認(rèn)設(shè)置。
要更改默認(rèn)的排序順序或字符集類,請(qǐng)使用——lc-collate和——lc-ctype選項(xiàng)。除了C或POSIX之外的排序順序也會(huì)造成性能損失。由于這些原因,在運(yùn)行initdb時(shí)選擇正確的語(yǔ)言環(huán)境是很重要的。
其余的語(yǔ)言環(huán)境類別可以在服務(wù)器啟動(dòng)后更改。還可以使用——locale設(shè)置所有語(yǔ)言環(huán)境類別的默認(rèn)值,包括排序順序和字符集類。所有服務(wù)器語(yǔ)言環(huán)境值(lc_*)都可以通過SHOW All來顯示。
常用參數(shù):
-D directory/--pgdata=directory此選項(xiàng)指定應(yīng)該存儲(chǔ)數(shù)據(jù)庫(kù)集群的目錄。必傳。也可以設(shè)置環(huán)境變量PGDATA來替換-D選項(xiàng)。
-U username/--username=username選擇數(shù)據(jù)庫(kù)superuser的用戶名。這默認(rèn)為運(yùn)行initdb的用戶的名稱。
-E encoding/--encoding=encoding選擇模板數(shù)據(jù)庫(kù)的編碼。這也是您稍后創(chuàng)建的任何數(shù)據(jù)庫(kù)的默認(rèn)編碼,
--lc-collate/--lc-ctype更改默認(rèn)的排序順序或字符集類。
-k/--data-checksums在數(shù)據(jù)頁(yè)上使用校驗(yàn)和來幫助檢測(cè)I/O系統(tǒng)的損壞,否則系統(tǒng)將是靜默的。啟用校驗(yàn)和可能會(huì)導(dǎo)致顯著的性能損失。如果設(shè)置,則計(jì)算所有數(shù)據(jù)庫(kù)中所有對(duì)象的校驗(yàn)和。所有校驗(yàn)和失敗將在pg_stat_database視圖中報(bào)告。
所以postgresql初始化可能使用的命令是:
su - pguser001 -c "/u01/pgsql/bin/initdb --username=pguser001 --encoding=UTF8 --lc-collate=C --lc-ctype=en_US. utf8 --data-checksums -D /data"
注:如果數(shù)據(jù)目錄已經(jīng)存在且初始化了的話,initdb將不會(huì)運(yùn)行。
補(bǔ)充:postgresql 初始化 initdb 出現(xiàn)No such file or directory
1.initdb出現(xiàn)
FATAL: could not open extension control file "/opt/HighGo/Develop/share/postgresql/extension/file_fdw.control": No such file or directory.
如下:
me@me:/opt/HighGo/Develop/bin$ <span style="color:rgb(51,51,255);">./initdb -D ../data -W</span>
The files belonging to this database system will be owned by user "me". 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". creating directory ../data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 32MB creating configuration files ... ok creating template1 database in ../data/base/1 ... ok initializing pg_authid ... ok Enter new systemuser password: Enter it again: Enter syssao password: Enter it again: Enter syssso password: Enter it again: setting dba password ... ok initializing dependencies ... ok <span style="color:rgb(204,0,0);">creating system views ... FATAL: could not open extension control file "/opt/HighGo/Develop/share/postgresql/extension/file_fdw.control": No such file or directory STATEMENT: /*</span> * PostgreSQL System Views * * Copyright (c) 1996-2012, PostgreSQL Global Development Group * * src/backend/catalog/system_views.sql
原因是file_fdw.control插件沒有安裝.進(jìn)入源代碼目錄下的contrib目錄下.找到file_fdw文件進(jìn)入其中,編譯安裝,
make make install
同理出現(xiàn):
creating system views ... FATAL: could not open extension control file "/opt/HighGo/Develop/share/postgresql/extension/dblink.control": No such file or directory.
在contrib目錄下進(jìn)入到dblink文件中,make,make install
所以如果出現(xiàn)一些其他的hstore.control 等.相應(yīng)的安裝上.
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL進(jìn)行數(shù)據(jù)導(dǎo)入和導(dǎo)出的操作代碼
在數(shù)據(jù)庫(kù)管理中,數(shù)據(jù)的導(dǎo)入和導(dǎo)出是非常常見的操作,特別是在 PostgreSQL 中,提供了多種工具和方法來實(shí)現(xiàn)數(shù)據(jù)的有效管理,本文將詳細(xì)介紹在 PostgreSQL 中如何進(jìn)行數(shù)據(jù)導(dǎo)入和導(dǎo)出,并給出具體的命令及示例,需要的朋友可以參考下2024-10-10
Postgresql之時(shí)間戳long,TimeStamp,Date,String互轉(zhuǎn)方式
這篇文章主要介紹了Postgresql中的時(shí)間戳long,TimeStamp,Date,String互轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
PostgreSQL中rank()窗口函數(shù)實(shí)用指南與示例
在數(shù)據(jù)分析和數(shù)據(jù)庫(kù)管理中,經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行排名操作,PostgreSQL提供了強(qiáng)大的窗口函數(shù)rank(),可以方便地對(duì)結(jié)果集中的行進(jìn)行排名,本文將詳細(xì)介紹rank()函數(shù)的使用方法,并通過多個(gè)實(shí)用示例展示其在不同場(chǎng)景下的應(yīng)用,需要的朋友可以參考下2025-07-07
Docker環(huán)境實(shí)現(xiàn)PostgreSQL自動(dòng)備份的流程步驟
本文詳細(xì)介紹了如何在Ubuntu系統(tǒng)中安裝Docker,然后在Docker容器內(nèi)安裝和配置PostgreSQL數(shù)據(jù)庫(kù),接著,重點(diǎn)講解了如何在PostgreSQL中安裝和配置pg_rman工具,用于數(shù)據(jù)庫(kù)的備份和恢復(fù)操作,文章還涵蓋了創(chuàng)建定時(shí)備份任務(wù)以及刪除備份的步驟,需要的朋友可以參考下2024-11-11
PostgreSQL如何查詢表大小(單獨(dú)查詢和批量查詢)
PostgreSQL提供了多個(gè)系統(tǒng)管理函數(shù)來查看表,索引表空間及數(shù)據(jù)庫(kù)的大小,這篇文章主要給大家介紹了關(guān)于PostgreSQL如何查詢表大小的相關(guān)資料,文中介紹的方法包括單獨(dú)查詢和批量查詢,需要的朋友可以參考下2024-02-02

