下载安装包
https://www.postgresql.org/download/
选择对应版本
安装
#yum源
yum -y install wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
#禁用内置的PostgreSQL模块:
yum -qy module disable postgresql
#安装postgresql16
yum install -y postgresql16-server
初始化数据库并启用自动启动:
/usr/pgsql-16/bin/postgresql-16-setup initdb
systemctl enable postgresql-16
systemctl start postgresql-16
已安装组件
[root@server08 bin]# rpm -aq| grep postgres
postgresql16-server-16.3-3PGDG.rhel8.x86_64
postgresql16-libs-16.3-3PGDG.rhel8.x86_64
postgresql16-16.3-3PGDG.rhel8.x86_64
登录
[root@server08 ~]# su - postgres
[postgres@server08 ~]$ psql
psql (16.3)
Type "help" for help.
postgres=#
修改密码
[postgres@server08 ~]$ psql
psql (16.3)
Type "help" for help.
postgres=#ALTER USER postgres WITH PASSWORD 'postgres';
登录
psql -U postgres -h 127.0.0.1 -p 5432 -W
修改远程连接
#修改pg远程连接配置文件
[root@server08 ~]# vim /var/lib/pgsql/16/data/pg_hba.conf
host all all 0.0.0.0/0 md5
#修改主配置文件
[root@server08 ~]# vim /var/lib/pgsql/16/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 2000 # (change requires restart)
#重启
systemctl restart postgresql-16
创建数据库以及表
psql -U postgres -h 127.0.0.1 -p 5432 -W
#创建库
postgres-# CREATE DATABASE mydb
OWNER postgres
ENCODING 'UTF8';
#切到mydb
postgres-# \c mydb
#创建表
postgres-# CREATE TABLE cities (
name varchar(80),
location point
);
配置文件功能介绍
[root@test19-server08 data]# ls /var/lib/pgsql/16/data
drwx------ 6 postgres postgres 4.0K Jul 12 14:36 base
-rw------- 1 postgres postgres 30 Jul 12 00:00 current_logfiles
drwx------ 2 postgres postgres 4.0K Jul 12 14:37 global
drwx------ 2 postgres postgres 4.0K Jul 12 00:00 log
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_commit_ts
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_dynshmem
-rw------- 1 postgres postgres 5.5K Jul 9 16:20 pg_hba.conf
-rw------- 1 postgres postgres 2.6K Jul 9 15:31 pg_ident.conf
drwx------ 4 postgres postgres 4.0K Jul 12 14:56 pg_logical
drwx------ 4 postgres postgres 4.0K Jul 9 15:31 pg_multixact
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_notify
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_replslot
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_serial
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_snapshots
drwx------ 2 postgres postgres 4.0K Jul 9 16:25 pg_stat
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_stat_tmp
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_subtrans
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_tblspc
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_twophase
-rw------- 1 postgres postgres 3 Jul 9 15:31 PG_VERSION
drwx------ 3 postgres postgres 4.0K Jul 9 15:31 pg_wal
drwx------ 2 postgres postgres 4.0K Jul 9 15:31 pg_xact
-rw------- 1 postgres postgres 88 Jul 9 15:31 postgresql.auto.conf
-rw------- 1 postgres postgres 29K Jul 9 16:24 postgresql.conf
-rw------- 1 postgres postgres 58 Jul 9 16:25 postmaster.opts
-rw------- 1 postgres postgres 94 Jul 9 16:25 postmaster.pid
#postgresql.conf
这是 PostgreSQL 的主配置文件,包含了大量可调节的运行时参数,用于控制数据库服务器的各个方面,包括但不限于内存分配、磁盘I/O、网络设置、查询优化、日志记录、安
全性和性能参数。这些参数可以控制数据库服务器的启动选项和运行时行为。
#pg_hba.conf
这个文件用于配置主机基于地址的认证(Host-Based Authentication),定义了哪些客户端可以连接到数据库服务器,以及它们使用哪种认证方法(如信任、密码、MD5、证书等)
它是控制数据库服务器访问安全的关键文件。
#pg_ident.conf
此文件用于用户名称映射,它允许将外部认证系统(如 LDAP 或者系统用户账户)的用户名映射到 PostgreSQL 数据库角色。这对于整合外部认证系统非常有用。
#postgresql.auto.conf
当 postgresql.conf 被重新加载时,任何在运行时动态更改的设置都会被写入这个文件。这有助于保留动态更改的参数,以便在下次服务器启动时仍然有效。
psql快捷命令
快捷命令 | 别名 | 描述 |
---|---|---|
\c | \connect | 连接到指定的数据库 |
\q | \quit | 退出 psql 客户端 |
\list | \l | 列出所有可用的数据库 |
\dt | 列出当前数据库中的所有表 | |
\d | 描述指定的表结构 | |
\dv | \views | 列出所有的视图 |
\df | \functions | 列出所有的函数 |
\dS | \sequences | 列出所有的序列 |
\dr | \rules | 列出所有的规则 |
\ds | \schemas | 列出所有的模式 |
\du | \users | 列出所有的用户和角色 |
\dp或\z | 查看表权限分配权限 | |
\da | \a | 列出所有的活动会话 |
\dT | \types | 列出所有的数据类型 |
\dI | \indexes | 列出所有的索引 |
\dC | \constraints | 列出所有的约束 |
\help | \? | 显示所有可用的元命令 |
\echo | \e | 输出一条信息 |
\timing | 开启或关闭命令执行时间的显示 | |
\set | 设置一个变量 | |
\unset | 取消设置一个变量 | |
\include | \i | 包含一个文件的内容 |
\watch | 监视表的变化 | |
\unwatch | 取消监视表的变化 | |
\copy | 用于数据导入导出的 COPY 命令的简化形式 | |
\password | 更改用户密码 | |
安装报错
[root@ PostgreSQL]# yum install -y postgresql16-server
PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 3 - x86_64 23 B/s | 146 B 00:06
Errors during downloading metadata for repository 'pgdg-common':
- Status code: 404 for https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-3-x86_64/repodata/repomd.xml (IP: 72.32.157.246)
Error: Failed to download metadata for repo 'pgdg-common': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
解决方法:修改配置文件添加pgdg-redhat-all.repo
vim /etc/yum/pluginconf.d/releasever_adapter.conf
[reposlist]
include=docker-ce.repo, epel.repo,pgdg-redhat-all.repo
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有