文章目录
- 一、Linux 下安装 postgreSQL 数据库
- 1.1、准备环境
- 1.2、关闭防火墙跟SELinux
- 1.2.1、关闭防火墙 firewalld
- 1.2.2、关闭SELinux
- 1.3、挂载本地镜像
- 1.4、软件包的下载postgreSQL
一、Linux 下安装 postgreSQL 数据库
1.1、准备环境
操作系统 | IP | 应用 |
---|---|---|
Red Hat 8 | 192.168.192.165 | postgreSQL15 |
1.2、关闭防火墙跟SELinux
1.2.1、关闭防火墙 firewalld
[root@localhost ~]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.5 (Ootpa)
[root@localhost ~]#
[root@localhost ~]# systemctl status firewalld // 查看 firewalld 状态
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-07-11 09:11:53 CST; 19min ago
Docs: man:firewalld(1)
Main PID: 1071 (firewalld)
Tasks: 2 (limit: 21837)
Memory: 33.0M
CGroup: /system.slice/firewalld.service
└─1071 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
···省略N
[root@localhost ~]#
[root@localhost ~]# systemctl disable --now firewalld // 关闭防火墙,并关闭开机自启功能
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]#
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
···省略N
[root@localhost ~]#
1.2.2、关闭SELinux
[root@localhost ~]# getenforce
Enforcing // 表示SELinux启动
[root@localhost ~]#
[root@localhost ~]# grep '^SELINUX=' /etc/selinux/config
SELINUX=enforcing // 修改enforcing 为 disabled
[root@localhost ~]#
[root@localhost ~]# sed -i '/SELINUX=enforcing/c SELINUX=disabled' /etc/selinux/config // 开关SELinux 开机自启。重启才能生效
[root@localhost ~]#
[root@localhost ~]# grep '^SELINUX=' /etc/selinux/config
SELINUX=disabled
[root@localhost ~]#
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]#
[root@localhost ~]# setenforce 0 // 临时关闭SELinux
[root@localhost ~]#
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]#
1.3、挂载本地镜像
[root@localhost ~]# mount /dev/cdrom /mnt // 挂载磁盘
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.7G 0 1.7G 0% /dev
tmpfs 1.7G 0 1.7G 0% /dev/shm
tmpfs 1.7G 9.4M 1.7G 1% /run
tmpfs 1.7G 0 1.7G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 46G 4.8G 41G 11% /
/dev/sda1 1014M 257M 758M 26% /boot
tmpfs 347M 12K 347M 1% /run/user/42
tmpfs 347M 0 347M 0% /run/user/0
/dev/sr0 11G 11G 0 100% /mnt // 表示挂载成功
[root@localhost ~]#
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# vi redhat8.repo // 配置本地 yum 源文件
[BaseOS]
name=baseos
baseurl=file:///mnt/BaseOS
gpgcheck=0
enabled=1
[AppStream]
name=appstream
baseurl=file:///mnt/AppStream
gpgcheck=0
enabled=1
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# dnf clean all // 清楚缓存
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
0 files removed
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# dnf makecache // 建立缓存
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
baseos 161 MB/s | 2.4 MB 00:00
appstream 153 MB/s | 7.2 MB 00:00
Metadata cache created.
[root@localhost yum.repos.d]#
1.4、软件包的下载postgreSQL
postgreSQL的rpm包下载
[root@localhost ~]# cd /opt/
[root@localhost opt]#
[root@localhost opt]# dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm // 安装 postgreSQL的rpm包
···省略N
[root@localhost opt]# rpm -qa | grep pgdg-redhat-repo
pgdg-redhat-repo-42.0-32.noarch
[root@localhost opt]#
// 安装PostgreSQL
[root@localhost ~]# dnf install -y postgresql15-server
...省略N
[root@localhost ~]# rpm -qa | grep postgresql15-server
postgresql15-server-15.3-2PGDG.rhel8.x86_64
[root@localhost ~]#
// 初始化数据库并启用开机自启动
[root@localhost ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK
[root@localhost ~]#
[root@localhost ~]# systemctl status postgresql-15 // 查看postgresql状态
● postgresql-15.service - PostgreSQL 15 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-15.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://www.postgresql.org/docs/15/static/
[root@localhost ~]#
[root@localhost ~]# systemctl enable --now postgresql-15 // 启动postgresql服务,并且开启开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-15.service → /usr/lib/systemd/system/postgresql-15.service.
[root@localhost ~]#
[root@localhost ~]# systemctl status postgresql-15
● postgresql-15.service - PostgreSQL 15 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-15.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2023-07-11 10:53:41 CST; 2s ago
Docs: https://www.postgresql.org/docs/15/static/
Process: 33969 ExecStartPre=/usr/pgsql-15/bin/postgresql-15-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 33975 (postmaster)
Tasks: 7 (limit: 21837)
Memory: 17.4M
CGroup: /system.slice/postgresql-15.service
├─33975 /usr/pgsql-15/bin/postmaster -D /var/lib/pgsql/15/data/
├─33976 postgres: logger
├─33977 postgres: checkpointer
├─33978 postgres: background writer
├─33980 postgres: walwriter
├─33981 postgres: autovacuum launcher
└─33982 postgres: logical replication launcher
...省略N
[root@localhost ~]# ss -antl // 查看postgreSQL端口是否存在:5432
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:5432 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:5432 [::]:*
[root@localhost ~]#
[root@localhost ~]# su - postgres // 登录postgres 用户
[postgres@localhost ~]$ psql // 登录到数据库里面
psql (15.3)
Type "help" for help.
postgres=#
postgres=# select version(); // 查看数据库版本
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-18), 64-bit
(1 row)
postgres=#
postgres=# select oid,datname,datdba from pg_database; // 查看当前有那些数据库
oid | datname | datdba
-----+-----------+--------
5 | postgres | 10
1 | template1 | 10
4 | template0 | 10
(3 rows)
postgres=# \l // 查看当前有那些数据库
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
(3 rows)
postgres=# exit
[postgres@localhost ~]$