欢迎阅读今天关于如何在 Ubuntu 20.04|22.04 上安装 PostgreSQL 数据库服务器的指南。PostgreSQL 是一种流行且功能强大的开源关系数据库管理系统,已被用于运行关键任务应用程序。PostgreSQL 基于 POSTGRES 4.2。
如果您想查看 PostgreSQL 数据库的所有酷特性,请访问Feature Metrix 页面以了解更多信息。本指南将直接介绍在 Ubuntu 20.04|22.04 Linux 系统上安装 PostgreSQL。
第 1 步:更新系统
我们需要在更新的系统上工作,以确保我们不会遇到任何依赖问题。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo apt update
sudo apt -y upgrade</code></span></span></span></span>
系统更新后,我们可以继续在 Ubuntu 20.04|22.04 Linux 上安装 PostgreSQL 数据库服务器。
第 2 步:在 Ubuntu 20.04|22.04 上安装 PostgreSQL 数据库服务器
我们将在 Ubuntu 20.04|22.04 上安装默认版本的 PostgreSQL 数据库服务器,而无需配置项目的上游存储库。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo apt install postgresql postgresql-client</code></span></span></span></span>
确认软件包安装以继续。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
apport apport-symptoms at bc bcache-tools bolt byobu cryptsetup cryptsetup-run ethtool fonts-ubuntu-console fwupd fwupd-signed git git-man htop
initramfs-tools-bin klibc-utils kpartx landscape-common libarchive13 liberror-perl libevent-2.1-7 libfl2 libfwupd2 libfwupdplugin1 libgcab-1.0-0
libgpgme11 libgusb2 libklibc libmspack0 libsgutils2-2 libsmbios-c2 libtss2-esys0 liburcu6 libutempter0 libxmlb1 libxmlsec1 libxmlsec1-openssl lz4
open-vm-tools pastebinit patch pollinate python3-apport python3-attr python3-automat python3-click python3-colorama python3-constantly
python3-debconf python3-debian python3-hamcrest python3-hyperlink python3-incremental python3-newt python3-problem-report python3-pyasn1
python3-pyasn1-modules python3-service-identity python3-systemd python3-twisted python3-twisted-bin python3-zope.interface run-one screen sg3-utils
sosreport tmux tpm-udev update-notifier-common
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libllvm9 libpq5 libsensors-config libsensors5 postgresql-12 postgresql-client-12 postgresql-client-common postgresql-common sysstat
Suggested packages:
lm-sensors postgresql-doc postgresql-doc-12 libjson-perl isag
The following NEW packages will be installed:
libllvm9 libpq5 libsensors-config libsensors5 postgresql postgresql-12 postgresql-client postgresql-client-12 postgresql-client-common
postgresql-common sysstat
0 upgraded, 11 newly installed, 0 to remove and 4 not upgraded.
Need to get 30.0 MB of archives.
After this operation, 116 MB of additional disk space will be used.
Do you want to continue? [Y/n] <strong>y</strong></code></span></span></span></span>
安装的 PostgreSQL 版本是12。这是撰写本文时最新的稳定版本。
该服务在安装时自动启动。您可以使用以下命令确认它是否正在运行:
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>$ <span style="color:var(--wp--preset--color--pale-pink) !important">systemctl status postgresql.service </span>
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2019-12-26 07:15:55 UTC; 1min 46s ago
Main PID: 3953 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 614)
Memory: 0B
CGroup: /system.slice/postgresql.service
Dec 26 07:15:55 ubuntu20 systemd[1]: Starting PostgreSQL RDBMS...
Dec 26 07:15:55 ubuntu20 systemd[1]: Started PostgreSQL RDBMS.</code></span></span></span></span>
第 3 步:更改服务侦听 IP(可选)
如果您需要网络应用程序连接到中央数据库,则需要更改listen_addresses行以允许绑定到服务器上可用的所有地址或特定 IP 地址。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code># Allow bind to all addresses
listen_addresses = '*'
# Allow bind to one IP addresses
listen_addresses = '192.168.10.20'</code></span></span></span></span>
对于多个 IP 地址,请列出它们并用逗号分隔。更改后,重新启动服务。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo systemctl restart postgresql </code></span></span></span></span>
第 4 步:更新 PostgreSQL 管理员用户的密码
postgresql 数据库管理员用户是在安装 PostgreSQL 数据库服务器时创建的。我们需要为这个用户设置一个安全密码。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo su - postgres
psql -c "alter user postgres with password 'MySt0ngDBP@ss'"</code></span></span></span></span>
尝试创建一个测试数据库和用户。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code><strong>createuser dbuser</strong>
<strong>createdb testdb -O dbuser</strong>
$ <strong>psql testdb </strong>
psql (12.1 (Ubuntu 12.1-1))
Type "help" for help.
testdb=# <strong>alter user dbuser with password 'StrongPassword'; </strong>
ALTER ROLE
testdb=# <strong>\q</strong>
postgres@ubuntu20:~$ <strong>dropdb testdb </strong></code></span></span></span></span>
列出创建的数据库:
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>$ <span style="color:var(--wp--preset--color--luminous-vivid-amber) !important">psql -l </span>
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | dbuser | UTF8 | C.UTF-8 | C.UTF-8 |
(4 rows)</code></span></span></span></span>
第 5 步:安装 phpPgAdmin 管理界面
我们现在可以安装可用于管理 PostgreSQL 数据库操作的 phpPgAdmin 管理界面。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo apt -y install phppgadmin php-pgsql </code></span></span></span></span>
要允许使用特权用户帐户(例如root或postgres )登录,请将以下行设置为 false。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>$ <span style="color:var(--wp--preset--color--pale-pink) !important">sudo vim /etc/phppgadmin/config.inc.php</span>
$conf['extra_login_security'] = false;</code></span></span></span></span>
要仅显示登录用户拥有的数据库,请设置以下行。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>$conf['owned_only'] = true;</code></span></span></span></span>
设置 IPv4 允许的本地连接。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>host all all 127.0.0.1/32 md5
host all all 192.168.10.0/24 md5
host all all 10.20.5.0/24 md5</code></span></span></span></span>
对 Web UI 访问执行相同的操作。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo nano /etc/apache2/conf-enabled/phppgadmin.conf </code></span></span></span></span>
只允许本地主机连接,添加其他 IP 地址,如下所示。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>Require local
Require ip 192.168.10.0/24
Require ip 10.10.0.0/24</code></span></span></span></span>
更改后重新启动 postgresql 服务。
<span style="color:#000000"><span style="color:#222222"><span style="background-color:#051e30"><span style="color:#ffffff"><code>sudo systemctl restart postgresql apache2</code></span></span></span></span>
要访问 phppgadmin 仪表板,请打开 URL http://(hostname_or_IP_address/phppgadmin/。
可用于学习 PostgreSQL 管理的 Udemy 课程:
- SQL 和 PostgreSQL:完整的开发人员指南
- 完整的 Python/PostgreSQL 课程 2.0
- 面向初学者的 SQL 和 PostgreSQL:成为 SQL 专家
- 使用 PostgreSQL 学习 SQL:从零到英雄
- PostgreSQL 训练营:从初学者到高级,60 多个小时
更多指南:
本教程详细介绍了如何在Ubuntu 20.04和22.04系统上安装PostgreSQL数据库服务器,包括更新系统、安装PostgreSQL、设置数据库服务监听IP、修改管理员密码以及安装phpPgAdmin管理界面。此外,还提供了设置IP限制和访问控制的步骤,以确保服务的安全性。
2113

被折叠的 条评论
为什么被折叠?



