Postgresql--初始使用者連線postgres以及使用pgadmin4連線 in Linux/打開Linux port

Posted by: Max Chen | in Postgresql | 1 year, 11 months ago |

Postgresql--初始使用者連線postgres以及使用pgadmin4連線 in Linux/打開Linux port

安裝好Postgresql後如何進入?

一開始會有初始使用者:postgres

可以通過以下登入

sudo -u postgres -i

可以通過以下來更改密碼

sudo -u postgres psql postgres

也可以通過以下創建新使用者

sudo su - postgres
CREATE ROLE username superuser PASSWORD 'yourpassword';
CREATE USER username; 
GRANT ROOT TO username;
ALTER ROLE username WITH LOGIN;

#修改密碼
ALTER USER yourusername WITH PASSWORD 'yournewpass';

注意通過“$ sudo passwd postgres”更改“postgres”的 UNIX 密碼的答案不是首選,甚至可能是危險的!

原因:默認情況下,UNIX 帳戶“postgres”是鎖定的,這意味著它不能使用密碼登錄。如果您使用“sudo passwd postgres”,該帳戶將立即解鎖。更糟糕的是,如果您將密碼設置為弱密碼,例如“postgres”,那麼您將面臨極大的安全隱患。例如,有許多機器人嘗試使用用戶名/密碼組合“postgres/postgres”來登錄您的 UNIX 系統。

您應該做的是遵循Chris James的回答:

sudo -u postgres psql postgres

password postgres

```

Enter new password:

稍微解釋一下。登錄PostgreSQL服務器通常有兩種默認方式:

通過以 UNIX 用戶身份運行“psql”命令(所謂的 IDENT/PEER 身份驗證),例如: sudo -u postgres psql。

請注意,sudo -u這不會解鎖 UNIX 用戶。

通過 TCP/IP 連接使用 PostgreSQL 自己管理的用戶名/密碼(所謂的 TCP 身份驗證)(即,不是UNIX 密碼)。

因此,您永遠不想為 UNIX 帳戶“postgres”設置密碼。默認情況下保持鎖定狀態。

當然,如果您將其配置為與默認設置不同,則情況可能會發生變化。

例如,可以將 PostgreSQL 密碼與 UNIX 密碼同步,並且只允許本地登錄。


使用pgadmin4連線

使用上述的postgres 帳密

這裡要注意幾點:

  1. 記得要把linux的5432 port打開, 操作如下

    檢查目前使用的port

    sudo lsof -i -P -n | grep LISTEN

    開啟防火牆5432

    sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent

    重新載入設定

    sudo firewall-cmd --reload

  2. 更改postgresql透過TCP/IP遠端連線設定 Server與client在不同機器,這時跑服務的機器就必須透過TCP/IP來存取資料庫,而SQL通常預設只允許在本地端存取,Postgresql也不例外,因此必須透過一點小設定將遠端存取的功能打開。

    查詢conf路徑

    psql -U postgres -c 'SHOW config_file'

    /var/lib/psql/13/data/postgresql.conf

    1.修改pg_hba.conf

    檔案位置: /var/lib/psql/13/data/pg_hba.conf 新增一行存取規則
    
    host all all 0.0.0.0/0 password
    參數1:host表遠端存取,local表本機端存取
    參數2:設定可存取的Database
    參數3:設定可存取的使用者
    參數4:設定可存取之網域,此設定全部網域皆可存取
    參數5:trust表不需認證,password表示需要密碼
    

    2.修改postgresql.conf

    取消註解並設定為 listen_addresses ='*'
    取消註解並設定為 port=5432
    

    3.重新啟動資料庫 (要記得)

官方流程, 還需要

sudo ufw allow postgres/tcp

/var/lib/psql/13/data/pg_hba.conf, 官方設定是這樣: host all all all md5

可能遇到問題:

Peer authentication failed for user “postgres”, when trying to get pgsql working with rails

The problem is still your pg_hba.conf file (/etc/postgresql/9.1/main/pg_hba.conf*).

This line:

local   all             postgres                                peer
Should be:

local   all             postgres                                md5

參考來源: https://learningsky.io/use-postgresql-databases-with-the-pgadmin/ https://zhidao.baidu.com/question/1899987672990115900.html https://serverfault.com/questions/110154/whats-the-default-superuser-username-password-for-postgres-after-a-new-install https://expect7.pixnet.net/blog/post/38215756 https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/ https://stackoverflow.com/questions/18664074/getting-error-peer-authentication-failed-for-user-postgres-when-trying-to-ge https://www.opencli.com/linux/rhel-centos-enable-firewall-ports https://blog.gtwang.org/linux/centos-7-firewalld-command-setup-tutorial/2/ https://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist https://docs.bitnami.com/installer/apps/canvaslms/administration/configure-pgadmin/ https://stackoverflow.com/questions/15008204/how-to-check-postgres-user-and-password

tags:Postgresql Linux
Currently unrated
 or 

Subscribe

* indicates required

Recent Posts

Archive

2023
2022
2021

Categories

Apache 1

Data Science 2

Dbfit 1

Design Pattern 1

Devops 4

DigitalOcean 1

Django 1

English 3

Excel 5

FUN 4

Flask 3

Git 1

HackMD 1

Heroku 1

Html/Css 1

Linux 4

MDX 1

Machine Learning 2

Manufacture 1

Master Data Service 1

Mezzanine 18

Oracle 1

Postgresql 7

PowerBI 4

Powershell 4

Python 22

SEO 2

SQL Server 53

SQL Server Analytics Service 1

SQLite 1

Windows 1

database 8

work-experience 1

其他 1

投資入門 1

投資心得 2

時間管理 1

總體經濟 2

自我成長 3

資料工程 1

Tags

SEO(1) Github(2) Title Tag(2) ML(1) 李宏毅(1) SQL Server(18) Tempdb(1) SSMS(1) Windows(1) 自我成長(2) Excel(1) python Flask(1) python(5) Flask(2)

Authors

Max Chen (159)

Feeds

RSS / Atom

Postgresql--初始使用者連線postgres以及使用pgadmin4連線 in Linux/打開Linux port

© COPYRIGHT 2011-2022. Max的文藝復興. ALL RIGHT RESERVED.