如何在Fedora 34上安裝和配置Postgres 14?
譯文【51CTO.com快譯】Postgresql是一種開源對(duì)象關(guān)系數(shù)據(jù)庫系統(tǒng),經(jīng)過30多年的積極開發(fā),在可靠性、功能穩(wěn)健性和性能方面已贏得了良好的聲譽(yù)。Postgres這個(gè)免費(fèi)的開源關(guān)系數(shù)據(jù)庫管理系統(tǒng)強(qiáng)調(diào)可擴(kuò)展性和SQL合規(guī)。
它最初名為POSTGRES,指其起源是加州大學(xué)伯克利分校開發(fā)的Ingres數(shù)據(jù)庫的后續(xù)版。PostgreSQL用作許多Web、移動(dòng)、地理空間和分析等應(yīng)用的主數(shù)據(jù)存儲(chǔ)或數(shù)據(jù)倉庫。PostgreSQL可以在單單一個(gè)產(chǎn)品中存儲(chǔ)結(jié)構(gòu)化數(shù)據(jù)和非結(jié)構(gòu)化數(shù)據(jù)。
本文介紹在Fedora 34中安裝Postgresql 14。
先決條件
要繼續(xù)進(jìn)行,確保您具備以下條件:
1. 更新后的Fedora 34服務(wù)器
2. 對(duì)服務(wù)器的root訪問權(quán)限或擁有root訪問權(quán)的用戶
3. 從服務(wù)器訪問互聯(lián)網(wǎng)
4. 對(duì)Linux終端有基本了解
1. 確保服務(wù)器是最新的
在繼續(xù)之前,確保我們的服務(wù)器有最新的軟件包。使用該命令:
- $ sudo dnf -y update
- Last metadata expiration check: 2:55:24 ago on Tue 26 Oct 2021 12:05:29 PM UTC.
- Dependencies resolved.
- Nothing to do.
- Complete!
2. 安裝和啟動(dòng)Postgres Server
fedora dnf postgres模塊含有Postgres 14安裝版,但不是默認(rèn)的。不妨使用該命令在Fedora上檢查Postges模塊:
- $ sudo dnf module list postgresql
- Last metadata expiration check: 2:56:45 ago on Tue 26 Oct 2021 12:05:29 PM UTC.
- Fedora Modular 34 - x86_64
- Name Stream Profiles Summary
- postgresql 9.6 client, server [d] PostgreSQL module
- postgresql 10 client, server [d] PostgreSQL module
- postgresql 11 client, server [d] PostgreSQL module
- postgresql 12 client, server PostgreSQL module
- postgresql 13 client, server PostgreSQL module
- Fedora Modular 34 - x86_64 - Updates
- Name Stream Profiles Summary
- postgresql 9.6 client, server [d] PostgreSQL module
- postgresql 10 client, server [d] PostgreSQL module
- postgresql 11 client, server [d] PostgreSQL module
- postgresql 12 client, server PostgreSQL module
- postgresql 13 client, server PostgreSQL module
- postgresql 14 client, server PostgreSQL module
- Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
這意味著要使用它,就得啟用它。先重置postgres模塊,那樣我們就沒有默認(rèn)版。
- sudo dnf module reset postgresql
然后用該命令啟用postgres 14:
- $ sudo dnf module enable postgresql:14
- Last metadata expiration check: 3:03:33 ago on Tue 26 Oct 2021 12:05:29 PM UTC.
- Dependencies resolved.
- ================================================================================================================================================================================================================================================================================
- Package Architecture Version Repository Size
- ========================================================================================================================================================================================================================================================================
- Enabling module streams:
- postgresql 14
- Transaction Summary
- ========================================================================================================================================================================================================================================================================
- Is this ok [y/N]: y
- Complete!
現(xiàn)在不妨安裝Postgresql 14和Contrib軟件包,后者為PostgreSQL數(shù)據(jù)庫系統(tǒng)提供了幾個(gè)額外的功能:
- sudo dnf install -y postgresql-server postgresql-contrib
一旦安裝完成,用以下命令初始化PostgreSQL數(shù)據(jù)庫:
- $ sudo postgresql-setup initdb
- WARNING: using obsoleted argument syntax, try --help
- WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
- * Initializing database in '/var/lib/pgsql/data'
- * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
現(xiàn)在啟動(dòng)服務(wù):
- sudo systemctl start postgresql
然后啟用該服務(wù),以便服務(wù)器重啟后它啟動(dòng):
- $ sudo systemctl enable postgresql
- Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
確認(rèn)Postgres 14在運(yùn)行:
- $ sudo systemctl status postgresql
- ● postgresql.service - PostgreSQL database server
- Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
- Active: active (running) since Tue 2021-10-26 15:18:53 UTC; 46s ago
- Process: 17645 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
- Main PID: 17647 (postmaster)
- Tasks: 8 (limit: 4603)
- Memory: 15.7M
- CPU: 47ms
- CGroup: /system.slice/postgresql.service
- ├─17647 /usr/bin/postmaster -D /var/lib/pgsql/data
- ├─17648 postgres: logger
- ├─17650 postgres: checkpointer
- ├─17651 postgres: background writer
- ├─17652 postgres: walwriter
- ├─17653 postgres: autovacuum launcher
- ├─17654 postgres: stats collector
- └─17655 postgres: logical replication launcher
- Oct 26 15:18:52 ip-10-2-40-182.us-west-2.compute.internal systemd[1]: Starting PostgreSQL database server...
- Oct 26 15:18:52 ip-10-2-40-182.us-west-2.compute.internal postmaster[17647]: 2021-10-26 15:18:52.998 UTC [17647] LOG: redirecting log output to logging collector process
- Oct 26 15:18:52 ip-10-2-40-182.us-west-2.compute.internal postmaster[17647]: 2021-10-26 15:18:52.998 UTC [17647] HINT: Future log output will appear in directory "log".
- Oct 26 15:18:53 ip-10-2-40-182.us-west-2.compute.internal systemd[1]: Started PostgreSQL database server.
Active: active (running)顯示該服務(wù)已啟動(dòng)并運(yùn)行中。
下一步,不妨通過連接至PostgreSQL數(shù)據(jù)庫服務(wù)器、打印輸出版本,確認(rèn)安裝已成功。 sudo -u postgres psql -c "SELECT version();"
輸出:
- $ sudo -u postgres psql -c "SELECT version();"
- could not change directory to "/home/fedora": Permission denied
- version
- --------------------------------------------------------------------------------------------------------------
- PostgreSQL 14.0 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1), 64-bit
- (1 row)
3. PostgreSQL角色和數(shù)據(jù)庫驗(yàn)證方法
PostgreSQL使用名為roles的概念來處理客戶身份驗(yàn)證和授權(quán)。默認(rèn)情況下,Postgres被設(shè)置為使用ident authentication,這意味著它將Postgres角色與匹配的Unix/Linux系統(tǒng)帳戶相關(guān)聯(lián)。如果Postgres中存在角色,有相同名稱的Unix/Linux用戶名就能以該角色登錄。
安裝過程創(chuàng)建了一個(gè)名為postgres的用戶帳戶,該帳戶與默認(rèn)的postgres角色相關(guān)聯(lián)。為了使用PostgreSQL,您可以登錄到該帳戶。
PostgreSQL支持多種驗(yàn)證方法。最常用的方法有:
- Trust——只要滿足pg_hba.conf中定義的條件,角色可以在沒有密碼的情況下進(jìn)行連接。
- Password——角色可以通過提供密碼進(jìn)行連接。密碼可以存儲(chǔ)為scram-sha-256、md5和password (clear-text)。
- Ident——僅在TCP/IP連接上受支持。它通過獲取客戶端的操作系統(tǒng)用戶名來工作,有可選的用戶名映射。
- Peer——與Ident一樣,但僅在本地連接上受支持。
4.連接到postgres數(shù)據(jù)庫
(1)通過切換到postres用戶
通過在終端中輸入以下內(nèi)容,切換到服務(wù)器上的postgres帳戶:
- sudo -i -u postgres
現(xiàn)在您可以立即訪問Postgres提示符,只需輸入:
- $ psql
- psql (14.0)
- Type "help" for help.
- postgres=#
您將因此登錄到PostgreSQL提示符,可以立即與數(shù)據(jù)庫管理系統(tǒng)進(jìn)行交互。
(2)通過以postgres用戶的身份來運(yùn)行命令
使用該方法,以postgres用戶的身份直接運(yùn)行命令,使用sudo:
- sudo -u postgres psql
輸出:
- $ sudo -u postgres psql
- psql (14.0)
- Type "help" for help.
- postgres=#
5. 配置postgres 14實(shí)例以便遠(yuǎn)程訪問
為此,我們將改動(dòng)postgres配置文件。我們需要打開文件,并調(diào)整配置。Postgresql 14的主配置文件位于該路徑:/var/lib/pgsql/14/data/pg_hba.conf。
不妨將peer識(shí)別改為trust(信任):
- sed -i '/^local/s/peer/trust/' /var/lib/pgsql/data/pg_hba.conf
將ident識(shí)別改為md5,允許密碼登錄。
- sed -i '/^host/s/ident/md5/' /var/lib/pgsql/data/pg_hba.conf
添加block,允許從所有地方訪問:
將該內(nèi)容添加到文件/var/lib/pgsql/data/pg_hba.conf
- host all all 0.0.0.0/0 md5
確保PostgreSQL在監(jiān)聽*
將這一行添加到這里的配置文件/var/lib/pgsql/data/postgresql.conf
- listen_addresses='*'
啟用并重啟postgresql服務(wù)器,重新加載配置:
- sudo systemctl restart postgresql
- sudo systemctl enable postgresql
6. 用戶管理
創(chuàng)建超級(jí)用戶
由于一切已到位,不妨創(chuàng)建超級(jí)用戶。
以postres角色連接到數(shù)據(jù)庫:
- $ sudo -u postgres psql
- psql (14.0)
- Type "help" for help.
- postgres=#
創(chuàng)建名稱為root的超級(jí)用戶:
- CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';
輸出:
- postgres=# CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';
- CREATE ROLE
- postgres=# \du
- List of roles
- Role name | Attributes | Member of
- -----------+------------------------------------------------------------+-----------
- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
- root | Superuser, Create role, Create DB | {}
- postgres=#
管理應(yīng)用程序用戶
以此創(chuàng)建數(shù)據(jù)庫、創(chuàng)建用戶,并向該用戶授予訪問該數(shù)據(jù)庫的所有權(quán)限:
- create database app_db_name;
- create user app_user with encrypted password 'dbpassword';
- grant all privileges on database app_db_name to app_user;
可在此查看用戶和權(quán)限管理方面的完整指南。
從遠(yuǎn)程主機(jī)連接到實(shí)例
使用該命令從本地機(jī)連接到postgres實(shí)例:
- psql 'postgres://<username>:<password>@<host>:<port>/<db>?sslmode=disable'
- # like
- psql 'postgres://root:passwordhere@192.160.1.20:5432/postgres?sslmode=disable'
結(jié)語
至此我們已在Fedora 34服務(wù)器上安裝了Postgresql 14,做一些基本的配置,然后做基本的用戶管理。
原文標(biāo)題:How to Install and Configure Postgres 14 on Fedora 34
【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】