如何在 Ubuntu 16.04 上更改 PostgreSQL 資料資料夾位置
在本文中,我們將學習如何在 Ubuntu 16.04 上將 PostgreSQL 資料庫資料目錄更改或重新定位到新位置。該資料庫的增長頻率較高,並且取決於公司規模,因為我們需要更多空間,並且出於安全原因,我們將資料目錄更改到其他卷或其他位置。
先決條件
- 具有 Sudo 許可權的非 root 使用者的 Ubuntu 機器。
- 已安裝並正在執行的 PostgreSQL 伺服器。
- 我們要將資料庫資料位置移動到的新卷或位置,新位置將為 /mnt/data_vol/PostgreSQL,其中 data_vol 是附加到機器或伺服器的新卷。
更改 PostgreSQL 資料資料夾位置
在更改 PostgreSQL 資料位置之前,我們將首先檢查 PostgreSQL 伺服器上的當前設定。以下是驗證伺服器上當前資料位置設定的命令。
$ sudo –u postgre psql Output: psql (9.5.4) Type "help" for help. postgres=#
驗證伺服器上當前資料位置設定
postgres=# SHOW data_directory; Output: data_directory ------------------------------ /var/lib/postgresql/9.5/main (1 row) postgres=#
透過以上命令,我們將瞭解預設資料目錄位置為 /var/lib/postgresql/9.5/main
現在,我們將停止 PostgreSQL 服務以更改資料資料夾的預設位置
以下是停止 PostgreSQL 服務的命令。
$ sudo systemctl stop postgresql
停止 PostgreSQL 後,我們將使用以下命令檢查 PostgreSQL 服務的狀態
$ sudo systemctl status postgresql Output: postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese Active: inactive (dead) since Mon 2016-09-12 15:40:23 IST; 3s ago Process: 1553 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 1553 (code=exited, status=0/SUCCESS) Sep 07 19:27:27 ubuntu-16 systemd[1]: Starting PostgreSQL RDBMS... Sep 07 19:27:27 ubuntu-16 systemd[1]: Started PostgreSQL RDBMS. Sep 12 15:20:23 ubuntu-16 systemd[1]: Stopped PostgreSQL RDBMS.
將現有 PostgreSQL 資料移動到新位置
由於我們已停止資料,因此我們現在將使用 rysnc 命令(帶 -a 和 –v 標誌)將現有 PostgreSQL 資料移動到新位置,-a 保留新位置的檔案和資料夾許可權,而 –v 將顯示詳細輸出。
使用 rsync,我們將在新位置建立一個新的 postgresql 資料夾,這裡的新位置是指在 /mnt/data_vol 處使用 data_vol 掛載的新卷,並且許可權將保留為 PostgreSQL,以便在將檔案複製到新位置時不會出現任何許可權問題。
以下是複製資料的命令。
$ sudo rsync -av /var/lib/postgresql /mnt/data_vol/ Output: sending incremental file list postgresql/ postgresql/.pgpass postgresql/.psql_history postgresql/9.5/ postgresql/9.5/main/ postgresql/9.5/main/PG_VERSION postgresql/9.5/main/postgresql.auto.conf postgresql/9.5/main/postmaster.opts postgresql/9.5/main/base/ postgresql/9.5/main/base/1/ postgresql/9.5/main/global/3592_vm postgresql/9.5/main/global/3593 postgresql/9.5/main/global/4060 postgresql/9.5/main/global/4060_vm postgresql/9.5/main/global/4061 postgresql/9.5/main/global/6000 postgresql/9.5/main/global/6000_vm postgresql/9.5/main/global/6001 postgresql/9.5/main/global/6002 postgresql/9.5/main/global/pg_control postgresql/9.5/main/global/pg_filenode.map … … postgresql/9.5/main/pg_subtrans/0000 postgresql/9.5/main/pg_tblspc/ postgresql/9.5/main/pg_twophase/ postgresql/9.5/main/pg_xlog/ postgresql/9.5/main/pg_xlog/000000010000000000000001 postgresql/9.5/main/pg_xlog/archive_status/ sent 63,180,570 bytes received 36,410 bytes 42,144,653.33 bytes/sec total size is 63,053,465 speedup is 1.00
複製資料庫後,我們將重新命名舊資料資料夾,並且我們將保留此資料夾,直到我們在後續步驟中確認更改,以便我們不會丟失機器上的資料。
$ sudo mv /var/lib/postgresql/9.5/main /var/lib/postgresql_backup
更改 Postgresql 配置檔案中的資料資料夾位置
我們可以透過編輯 /etc/postgresql/9.5/main/postgresql.conf 檔案並編輯 data_directory 來更改預設資料資料夾。
$ sudo vi /etc/postgresql/9.5/main/postgresql.conf Output: …. …. …. #------------------------------------------------------------------------------ # FILE LOCATIONS #------------------------------------------------------------------------------ # The default values of these variables are driven from the -D command-line # option or PGDATA environment variable, represented here as ConfigDir data_directory = '/mnt/data_vol/postgresql/9.5/main' # use data in another directory # (change requires restart) hba_file = '/etc/postgresql/9.5/main/pg_hba.conf' # host-based authentication file # (change requires restart) ident_file = '/etc/postgresql/9.5/main/pg_ident.conf' # ident configuration file …. …. ….
在 /etc/postgresql/9.5/main/postgresql.conf 中更新了新的資料資料夾後,我們需要重新啟動伺服器。
重新啟動 PostgreSQL 伺服器並驗證資料資料夾位置
由於我們必須更新 PostgreSQL 配置,因此我們將重新啟動 PostgreSQL 服務,以便應用配置。
$ sudo systemctl start postgresql
服務重新啟動後,我們現在將使用以下命令檢查 PostgreSQL 服務的狀態 –
$ sudo systemctl status postgresql postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pres Active: active (exited) since Mon 2016-09-12 16:57:32 IST; 12s ago Process: 22296 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 22296 (code=exited, status=0/SUCCESS) Sep 12 16:57:32 ubuntu-16 systemd[1]: Starting PostgreSQL RDBMS... Sep 12 16:57:32 ubuntu-16 systemd[1]: Started PostgreSQL RDBMS. Sep 12 16:57:39 ubuntu-16 systemd[1]: Started PostgreSQL RDBMS.
服務重新啟動後,我們現在將驗證資料資料夾位置。
$ sudo –u postgre psql Output: psql (9.5.4) Type "help" for help. postgres=#
驗證伺服器上當前資料位置設定 –
postgres=# SHOW data_directory; Output: data_directory ------------------------------ /mnt/data_vol/postgresql/9.5/main (1 row) postgres=#
確認 PostgreSQL 上的資料資料夾已更改後,我們現在將使用以下命令刪除舊資料。
$ sudo rm –rf /var/lib/postgresql_backup
在本文中,我們學習瞭如何將 PostgreSQL 資料資料夾更改為一個新的位置,我們可以在該位置儲存更多資料,並且可以將現有資料與資料庫一起移動到新位置。