PostgreSQL忘記postgres賬號密碼的解決方法
PostgreSQL簡介
PostgreSQL是一個功能非常強大的、源代碼開放的客戶/服務(wù)器關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS)。PostgreSQL最初設(shè)想于1986年,當時被叫做Berkley Postgres Project。該項目一直到1994年都處于演進和修改中,直到開發(fā)人員Andrew Yu和Jolly Chen在Postgres中添加了一個SQL(Structured Query Language,結(jié)構(gòu)化查詢語言)翻譯程序,該版本叫做Postgres95,在開放源代碼社區(qū)發(fā)放。
下面給大家介紹下PostgreSQL忘記postgres密碼的處理方法,具體內(nèi)容如下所示:
PostgreSQL數(shù)據(jù)庫中,假如你忘記了postgres賬號的密碼或者由于工作交接問題,等交接到你手頭的時候,沒有postgres賬號密碼,那怎么辦呢?其實不用慌,像MySQL、SQL Server等數(shù)據(jù)庫一樣,只要你擁有操作系統(tǒng)權(quán)限,修改postgres超級賬號的密碼也非常方便簡單。下面測試環(huán)境為CentOS Linux release 7.2.1511 (Core), PostgreSQL數(shù)據(jù)庫版本為9.5。其它不同版本的操作其實是一樣的,只是略有細微差別。
1:定位pg_hba.conf文件位置
首先找到pg_hba.conf文件的位置,具體有下面這些方法:
方法1:locate定位pg_hba.conf文件的位置
$ locate pg_hba.conf /usr/pgsql-9.5/share/pg_hba.conf.sample /var/lib/pgsql/9.5/data/pg_hba.conf
方法2:find命令查找。
$ find / -name "pg_hba.conf" 2>%1 | grep -v "Permission denied" /var/lib/pgsql/9.5/data/pg_hba.conf
2:修改pg_hba.conf配置文件
修改pg_hba.conf前最好做一個備份,這是一個良好的習慣,避免回滾的時候,你能輕松回撤所有操作。
#cp /var/lib/pgsql/9.5/data/pg_hba.conf /var/lib/pgsql/9.5/data/pg_hba.conf.20210125
在pg_hba.conf中找到類似下面這樣的地方:
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all md5 # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres peer #host replication postgres 127.0.0.1/32 ident #host replication postgres ::1/128 ident # Allow access from all host to connect to this UAT server host all all 0.0.0.0/0 md5
關(guān)于修改pg_hba.conf,如果你打算以socket方式在本機登錄數(shù)據(jù)庫,那么只需修改local這條記錄,將pg_hba.conf中的這個選項的的值從md5修改為trust
修改前 # "local" is for Unix domain socket connections only local all all md5 修改后 # "local" is for Unix domain socket connections only local all all trust
trust表示允許可信的本地連接。此時連接數(shù)據(jù)庫不用輸入密碼。
小知識:
TYPE表示主機類型,它的取值有下面這些:
local :表示是unix-domain的socket連接
host :表示TCP/IP socket
hostssl: 表示SSL加密的TCP/IP socket
如果你打算以TCP/IP方式訪問數(shù)據(jù)庫,即psql -h127.0.0.1 -Upostgres這樣的方式,那么必須修改host的配置。具體如下所示:
修改前: # IPv4 local connections: host all all 127.0.0.1/32 md5 修改后: # IPv4 local connections: host all all 127.0.0.1/32 trust
3: 重啟PostgreSQL服務(wù)
重啟PostgreSQL服務(wù)的方法很多,這里不做過多介紹,選擇你常用的方式即可。
# service postgresql-9.5 stop Redirecting to /bin/systemctl stop postgresql-9.5.service # service postgresql-9.5 start Redirecting to /bin/systemctl start postgresql-9.5.service # service postgresql-9.5 status Redirecting to /bin/systemctl status postgresql-9.5.service
4:重置賬號postgres的密碼
使用psql無密碼登錄,修改用戶postgres的密碼
alter user postgres with password '新的密碼';
方式1:
#psql -U postgres
方式2:
#psql
關(guān)于兩者,如果ssh是用postgres用戶連接服務(wù)器的話,那么直接psql即可,如果是root用戶連接服務(wù)器的話,必須用psql -U postgres
-bash-4.2$ psql psql (9.5.6) Type "help" for help. postgres=# alter user postgres with password 'xxxxxxxx'; ALTER ROLE
5:恢復pg_hba.conf配置文件的修改
6:重啟PostgreSQL服務(wù)
到此這篇關(guān)于PostgreSQL忘記postgres賬號的密碼的解決方法的文章就介紹到這了,更多相關(guān)PostgreSQL忘記postgres密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postgresql pg_hba.conf 簡介及配置詳解
配置文件之pg_hba.conf該文件用于控制訪問安全性,管理客戶端對于PostgreSQL服務(wù)器的訪問權(quán)限,本文給大家介紹postgresql pg_hba.conf 簡介及配置,感興趣的朋友跟隨小編一起看看吧2024-03-03Postgresql數(shù)據(jù)庫character?varying和character的區(qū)別說明
這篇文章主要介紹了Postgresql數(shù)據(jù)庫character?varying和character的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Postgresql 數(shù)據(jù)庫轉(zhuǎn)義字符操作
這篇文章主要介紹了Postgresql 數(shù)據(jù)庫轉(zhuǎn)義字符操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL 實現(xiàn)定時job執(zhí)行(pgAgent)
這篇文章主要介紹了PostgreSQL 實現(xiàn)定時job執(zhí)行(pgAgent),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01