• 跳至主要導覽
  • 跳至主要內容
  • 跳至主要資訊欄
Office 指南

Office 指南

辦公室工作實用教學

  • Excel
  • Word
  • PowerPoint
  • Windows
  • PowerShell
  • R

Linux 清除 PostgreSQL 資料庫所有資料與設定教學

介紹如何在 Linux 系統中清除 PostgreSQL 資料庫的所有資料與設定,重設為全新的資料庫。


重設 PostgreSQL 資料庫的方法就是將現有的 PostgreSQL cluster 移除,然後再新增一個全新的 PostgreSQL cluster,這裡我們以 Ubuntu Linux 系統來示範,此方式對於其他不同的 Linux 發行版也同樣適用。

移除 PostgreSQL 資料庫 Cluster

Step 1
首先列出目前系統上所有的 PostgreSQL 資料庫 clusters:

列所有 PostgreSQL clusters
pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
10  main    5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log

一般的伺服器上通常只有一個 PostgreSQL cluster
Step 2
移除 PostgreSQL cluster 之前,要先停止系統的 PostgreSQL 資料庫服務:

# 停止系統 PostgreSQL 服務
sudo systemctl stop postgresql@10-main

Step 3
停止 PostgreSQL 服務之後,將舊的 PostgreSQL cluster 移除:

# 移除 PostgreSQL 10 main cluster
sudo pg_dropcluster --stop 10 main

新增 PostgreSQL 資料庫 Cluster

Step 1
重新建立一個新的 PostgreSQL cluster,資料庫的版本可以自行指定,但除非有特殊需要,不然就維持跟原來的版本相同即可:

# 建立新的 PostgreSQL 10 main cluster
sudo pg_createcluster --start 10 main

Step 2
啟動系統的 PostgreSQL 服務:

# 啟動系統 PostgreSQL 服務
sudo systemctl start postgresql@10-main

如果沒有出現錯誤訊息,這樣就完成重設 PostgreSQL 資料庫的動作了。
Step 3
切換成 postgres 使用者,並進入 PostgreSQL 資料庫的指令介面,進行後續的設定操作。

# 切換成 postgres 使用者,並進入 PostgreSQL 指令介面
sudo -u postgres psql

參考資料:StackOverflow

分類:資料庫 標籤:PostgreSQL

主要資訊欄

搜尋

近期文章

  • Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤
  • Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例
  • Linux 網路設定 ip 指令用法教學與範例
  • Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例
  • Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例
  • Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例
  • Linux 以 Cryptsetup 與 LUKS 加密磁碟教學與範例
  • Linux 使用 age 簡潔的加密、解密工具使用教學與範例

推薦網站

  • Udemy 線上教學課程
  • Coursera 線上教學課程

關注本站

  • 電子郵件
  • Facebook

公益

  • 家扶基金會
  • 台灣世界展望會
  • Yahoo 奇摩公益
  • igiving 公益網
  • 兒福聯盟

Copyright © 2021 · Office Guide