二进制文件就是安装包,从这里下载。
预先准备好一个Oracle Linux 9的虚机。对于PostgreSQL,Oracle Linux归入RedHat Linux一类:
然后进入下面页面,Linux downloads (Red Hat family)。
设置repository:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
安装postgres server:
$ sudo dnf install postgresql-server
Last metadata expiration check: 0:01:24 ago on Mon 10 Feb 2025 03:26:58 AM GMT.
Dependencies resolved.
==============================================================================================================================================================================================
Package Architecture Version Repository Size
==============================================================================================================================================================================================
Installing:
postgresql-server x86_64 13.18-1.el9_5 ol9_appstream 6.0 M
Installing dependencies:
postgresql x86_64 13.18-1.el9_5 ol9_appstream 1.8 M
postgresql-private-libs x86_64 13.18-1.el9_5 ol9_appstream 136 k
Transaction Summary
==============================================================================================================================================================================================
Install 3 Packages
Total download size: 7.9 M
Installed size: 29 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): postgresql-private-libs-13.18-1.el9_5.x86_64.rpm 753 kB/s | 136 kB 00:00
(2/3): postgresql-13.18-1.el9_5.x86_64.rpm 7.1 MB/s | 1.8 MB 00:00
(3/3): postgresql-server-13.18-1.el9_5.x86_64.rpm 18 MB/s | 6.0 MB 00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 23 MB/s | 7.9 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : postgresql-private-libs-13.18-1.el9_5.x86_64 1/3
Installing : postgresql-13.18-1.el9_5.x86_64 2/3
Running scriptlet: postgresql-server-13.18-1.el9_5.x86_64 3/3
Installing : postgresql-server-13.18-1.el9_5.x86_64 3/3
Running scriptlet: postgresql-server-13.18-1.el9_5.x86_64 3/3
Verifying : postgresql-13.18-1.el9_5.x86_64 1/3
Verifying : postgresql-private-libs-13.18-1.el9_5.x86_64 2/3
Verifying : postgresql-server-13.18-1.el9_5.x86_64 3/3
Installed:
postgresql-13.18-1.el9_5.x86_64 postgresql-private-libs-13.18-1.el9_5.x86_64 postgresql-server-13.18-1.el9_5.x86_64
Complete!
这一步完成后,用户postgres已创建,环境变量PGDATA已设置。版本是自动选择的,为13.18。你也可以在安装时指定版本。
接下来需要初始化和创建数据库,并配置服务:
sudo postgresql-setup --initdb
sudo systemctl enable postgresql.service
sudo systemctl start postgresql.service
概要输出如下:
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
...
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
安装日志如下:
# cat /var/lib/pgsql/initdb_postgresql.log
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/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... GMT
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
/usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start
安装的版本为如下:
$ psql --version
psql (PostgreSQL) 13.18
服务已启动:
$ sudo systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: disabled)
Active: active (running) since Mon 2025-02-10 03:33:17 GMT; 52s ago
Process: 48211 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
Main PID: 48213 (postmaster)
Tasks: 8 (limit: 100069)
Memory: 16.6M
CPU: 51ms
CGroup: /system.slice/postgresql.service
├─48213 /usr/bin/postmaster -D /var/lib/pgsql/data
├─48215 "postgres: logger "
├─48217 "postgres: checkpointer "
├─48218 "postgres: background writer "
├─48219 "postgres: walwriter "
├─48220 "postgres: autovacuum launcher "
├─48221 "postgres: stats collector "
└─48222 "postgres: logical replication launcher "
看下数据目录,数据库似乎不大:
$ sudo ls /var/lib/pgsql/data
base global pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf postmaster.opts
current_logfiles log pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf postmaster.pid
$ sudo du -sh /var/lib/pgsql/data
41M /var/lib/pgsql/data
最初,有3个数据库:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
创建数据库,失败了:
$ createdb
createdb: error: could not connect to database template1: FATAL: role "opc" does not exist
$ id -un
opc
最简单的方法是切换到postgres用户:
$ sudo su - postgres
$ createdb
createdb: error: could not connect to database template1: FATAL: role "opc" does not exist
$ createdb mydb
## $ dropdb mydb
访问数据库:
$ psql mydb
psql (13.18)
Type "help" for help.
mydb=# SELECT version();
version
-------------------------------------------------------------------------------------------------------------------
PostgreSQL 13.18 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 11.5.0 20240719 (Red Hat 11.5.0-2.0.1), 64-bit
(1 row)
mydb=# create table test(a int);
CREATE TABLE
mydb=# drop table test;
DROP TABLE
mydb=# exit