前言
最近有同学问到在 Ubuntu 下如何编译安装 PostgreSQL。这次内容就来说一下如何在 Ubuntu 操作系统中安装 PostgreSQL 数据库。那么,安装 PostgreSQL 对于 DBA 来说是再简单不过的事儿了,我们知道 PostgreSQL 在 Redhat 家族系统中有三种安装方式,分别是源码安装,rpm 方式安装和yum 方式安装,当然,rpm 安装和 yum 安装可以当做是同类型的安装,除此之外,就是使用已经编译好的二进制安装。那么对于Ubuntu操作系统来说,除了一键化使用 apt-get install 来安装和 redhat 家族安装不同以外,源码安装和二进制安装都一样。但如果使用源码安装,需要解决一些依赖。本文就是在最新版本的 Ubuntu 服务器操作系统上安装 PostgreSQL。
NOTE:当前 Ubuntu 操作系统上已经集成了最新的 PostgreSQL 安装包,版本为 12.9 版本
一 、 使用 apt-get 安装
apt-get 命令为 Ubuntu 系统管理软件的命令,利用该命令可以管理,移除,清空,检查等在 Ubuntu 上安装的软件。
1. 操作系统版本
操作系统为 Ubuntu 20.04.3 服务器操作系统
root@developer:~# lsb_release -a
No LSB modules are available.
Distributor ID:Ubuntu
Description:Ubuntu 20.04.3 LTS
Release:20.04
Codename:focal
2. 查看 Ubuntu 上可用的 PostgreSQL 包
通过 apt list 可以列出 PostgreSQL 相关的软件包
root@developer:~# apt list | grep -w postgresql-12 | tail -1
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
postgresql-12/focal-updates,focal-security 12.9-0ubuntu0.20.04.1 amd64
3. 执行安装
postgresql-12/focal-updates,focal-security 12.9-0ubuntu0.20.04.1 amd64
root@developer:~# apt-get install postgresql-12 -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
postgresql-12
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 0 B/13.5 MB of archives.
After this operation, 41.1 MB of additional disk space will be used.
Preconfiguring packages ...
Selecting previously unselected package postgresql-12.
(Reading database ... 158185 files and directories currently installed.)
Preparing to unpack .../postgresql-12_12.9-0ubuntu0.20.04.1_amd64.deb ...
Unpacking postgresql-12 (12.9-0ubuntu0.20.04.1) ...
Setting up postgresql-12 (12.9-0ubuntu0.20.04.1) ...
Creating new PostgreSQL cluster 12/main ...
/usr/lib/postgresql/12/bin/initdb -D /var/lib/postgresql/12/main --auth-local peer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/12/main ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128