【01】openEuler 源码安装 PostgreSQL

openEuler 源码安装 PostgreSQL

  • 部署环境说明
  • Shell 前端软件包管理器基础概念
    • YUM 简介
    • DNF 简介
  • 源码安装 PostgreSQL
    • 环境变量(env)设置
      • 临时环境变量设置
      • 永久环境变量设置
    • 初始化数据库(initdb)
  • 数据库基本操作
    • 数据库基本配置(postgresql.conf)
    • 启动、停止、查看数据库
    • 使用 psql 登录数据库
    • 查看数据库版本信息
    • 更多(postgres/pg_ctl)命令说明

部署环境说明

  • Linux 系统:openEuler 22.03 LTS SP3 x86_64(下载地址:openEuler下载 | 欧拉系统ISO镜像 | openEuler社区官网)

  • 数据库:postgresql-15.6(下载地址:https://ftp.postgresql.org/pub/source/v15.6/postgresql-15.6.tar.gz)

pgsql

Shell 前端软件包管理器基础概念

YUM 简介


YUM(全称为 Yellow dog Updater, Modified)是一个在 FedoraRHEL、CentOS、OEL 中的 Shell 前端软件包管理器。

YUM 本身基于 RPM 包管理,能够从指定的 YUM 源服务器(一个或多个)自动下载 RPM 包并且进行安装和更新,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。

要成功的使用 YUM 工具安装更新软件或系统,就需要有一个包含各种 RPM 软件包的 repository(软件仓库),这个软件仓库我们习惯称为 YUM 源 (可以是本地源、网络源)。

DNF 简介


DNF(全称为 Dandified yum)是新一代的 rpm 软件包管理器,他首先出现在 Fedora 18 这个发行版中。而最近,它取代了 yum,正式成为 Fedora 22 的包管理器。

DNFRHEL、CentOS、OEL 等系统中,从版本 8 开始出现,目前和 YUM 共存。

DNF 克服了 YUM 包管理器的一些瓶颈,提升了包括用户体验,内存占用,依赖分析,运行速度等多方面的内容。

DNF 使用 Hawkey 库,该库解析 RPM 依赖性以在客户端计算机上运行查询。 它们基于 libsolv 构建,libsolv 是一种使用可满足性算法的程序包相关性求解器。 您可以在 libsolvGitHub 存储库中找到有关该算法的更多详细信息。

源码安装 PostgreSQL

  1. 查看 linux 软件源。
vi /etc/dnf/dnf.conf
[repo-id 名称]
name=取个名字随意(通常和 repo-id 名称相同)
baseurl=软件源地址# 或者vi /etc/yum.repos.d/openEuler.repo[OS]
name=OS
baseurl=http://repo.openeuler.org/openEuler-22.03-LTS-SP3/OS/$basearch/
metalink=https://mirrors.openeuler.org/metalink?repo=$releasever/OS&arch=$basearch
metadata_expire=1h
enabled=1
gpgcheck=1
gpgkey=http://repo.openeuler.org/openEuler-22.03-LTS-SP3/OS/$basearch/RPM-GPG-KEY-openEuler

参考:设置 openEuler(欧拉系统)安装源

  1. 安装编译环境依赖包。
# yum 安装
sudo yum install -y systemtap-sdt-devel.x86_64 perl-ExtUtils-Embed bzip2 readline readline-devel lz4 lz4-devel openssl openssl-devel pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel tcl tcl-devel openldap openldap-devel python3 python3-devel kernel-headers autoconf proj.x86_64 vim nc wget psmisc gcc-c++ gcc lrzsz make cmake telnet net-tools bind-utils tree cifs-utils ntpdate bash-completion sysstat iotop iftop htop unzip nmap bc bind-utils nethogs# dnf 安装
sudo dnf install -y perl-ExtUtils-Embed readline-devel python3-devel pam-devel libxml2-devel libxslt-devel openldap-devel lz4-devel llvm-devel systemd-devel container-selinux selinux-policy-devel openssl-devel gcc-c++ gcc cmake

说明:openEuler 使用 dnf 作为默认包管理工具。

  1. 下载 postgresql 源码。
// 1. 切换到 /opt/postgresql 目录下
cd /opt/postgresql
// 2. 使用命令下载 postgresql
wget https://ftp.postgresql.org/pub/source/v15.6/postgresql-15.6.tar.gz
// 3. 解压文件
sudo tar -zxvf postgresql-15.6.tar.gz
// 4. 创建文件夹目录(用来存放安装 postgresql 的相关文件)
sudo mkdir -p /pgccc/pgdata/data
  1. 编译和安装 postgresql
// 1. 检测系统环境并生成 Makefile 文件,prefix 默认安装路径 /opt/postgresql/pgsql
./configure --prefix=/pgccc/pgdata --with-perl --with-python --with-pam --with-libxml --with-libxslt --with-ldap --with-lz4 --with-llvm --with-systemd --with-selinux --with-openssl // 2. 编译 & 安装
gmake world && gmake install-world

gmakegmake install 是两个命令,参数说明:

  • gmake,编译,依据 Makefile 文件把源码包编译成二进制可执行文件。

  • gmake install 安装的意思。

gmake && gmake install 的意思就是执行 gmake 如果没有发生错误就执行 gmake install

  1. 查看 postgresql 安装目录。
ls -al /pgccc/pgdata/

输出信息:

[root@euler /]# ls -al /pgccc/pgdata/
总用量 28
drwxr-xr-x. 7 root root 4096  229 20:28 .
drwxr-xr-x. 3 root root 4096  229 20:11 ..
drwxr-xr-x. 2 root root 4096  229 20:17 bin
drwxr-xr-x. 2 root root 4096  229 20:28 data
drwxr-xr-x. 4 root root 4096  229 20:17 include
drwxr-xr-x. 4 root root 4096  229 20:17 lib
drwxr-xr-x. 5 root root 4096  229 20:

文件目录说明:

  • bin 存放二进制文件;
  • include 存放 .h 头文件;
  • lib 存放安装所需的各种依赖库,动态库;
  • share 存放所需的插件(extension),组件;

环境变量(env)设置

临时环境变量设置

  1. 编写 shell 脚本:
vi pgsql-15.6-env.sh
# 写入环境变量配置信息
export PGHOME=/pgccc/pgdata
export PGHOST=localhost
export PATH=$PGHOME/bin:$PATH:$HOME/bin
#export PATH=/pgccc/pgdata/bin:$PATH 
export LD_LIBRARY_PATH=/pgccc/pgdata/lib:$LD_LIBRARY_PATH 
export PGDATA=/pgccc/pgdata/data
export PGPORT=5432 
export PGUSER=postgres
  1. 执行命令,运行该文件(临时生成一下):
source pgsql-15.6-env.sh
  1. 查看初始化 db 版本信息,目的验证环境变量是否生效:
initdb --version
  1. 查看当前系统使用编码集:
echo $LANG

永久环境变量设置

环境变量配置文件 profile (不推荐全局修改)。可以使用命令输出当下用户环境变量信息:

env 或 peintenv

参考:Linux系统中.bash_profile文件详解_Linux_脚本之家 (jb51.net)

初始化数据库(initdb)

注意:源码安装PostgreSQL 数据库,没有默认的 postgres 用户,需自行手动创建用户组和用户,并设置密码。

原因:root 不能执行 PostgreSQL 的一些命令,因此要创建 postgres 这个用户。

  • 初始化数据库
initdb -D /pgccc/pgdata/data

注意:使用非 root 授权用户执行初始化数据库命令。

若出现如下错误信息:

initdb: error: cannot be run as root
initdb: hint: Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.

解决办法:

# 添加非 root 用户 postgres
sudo useradd postgres
# 给 postgres 用户设置密码
sudo passwd postgres
# 在相对应目录创建文件夹
sudo mkdir /pgccc/pgdata/data
# 给 postgres 用户授权 data 目录可执行权限
sudo chown -R postgres:postgres /pgccc/pgdata/data
# 初始化数据库实例
initdb -D /pgccc/pgdata/data -U postgres
# 启动数据库实例
pg_ctl start -D /pgccc/pgdata/data -l logfile 
# 查看启动日志文件信息
cat ./logfile
  • 查看更多 initdb 命令帮助信息:
initdb --help

数据库基本操作

数据库基本配置(postgresql.conf)

  1. 配置数据库监听 IP 和端口(port):
vi /pgccc/pgdata/data/postgresql.conf

修改 listen_addressesport

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------# - Connection Settings -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 = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories# (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation# (change requires restart)
#bonjour = off                          # advertise server via Bonjour# (change requires restart)
#bonjour_name = ''                      # defaults to the computer name# (change requires restart)

修改说明:

  • listen_addresses 默认值 localhost (只允许本地登录),配置为 “*” 代表在本机的所有地址上监听。

  • port 默认值 5432,如果安装了多个数据库实例,则需要为每个实例指定不同的监听端口。

  1. 配置数据库错误日志
#------------------------------------------------------------------------------
# REPORTING AND LOGGING
#------------------------------------------------------------------------------# - Where to Log -#log_destination = 'stderr'             # Valid values are combinations of# stderr, csvlog, jsonlog, syslog, and# eventlog, depending on platform.# csvlog and jsonlog require# logging_collector to be on.# This is used when logging to stderr:
logging_collector = on                  # Enable capturing of stderr, jsonlog,# and csvlog into log files. Required# to be on for csvlogs and jsonlogs.# (change requires restart)# These are only used if logging_collector is on:
log_directory = 'pg_log'                # directory where log files are written,# can be absolute or relative to PGDATA
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,# can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,# begin with 0 to use octal notation
#log_rotation_age = 1d                  # Automatic rotation of logfiles will# happen after that time.  0 disables.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will# happen after that much log output.# 0 disables.

修改说明:

  • logging_collector = on ,默认为 off

  • log_directory = 'pg_log' ,默认为 log(相对路径,即 ${PGDATA}/pg_log)。也可以改为绝对路径,还可以定义在其他目录或者分区,但是必须先创建此目录,并且该目录有修改权限。

  • log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

参考:PostgreSQL 日志参数解释 常用环境日志参数配置_log_min_duration_statement-CSDN博客

启动、停止、查看数据库

可能出现类似的异常信息

  • 异常一:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

解决方案,改用非 root 账号执行 pt_ctl 相关命令即可。

su postgres
  • 异常二:
[jeff@euler ~]$ pg_ctl --help
-bash: pg_ctl:未找到命令

解决方案,由于上面 postgresql 环境变量配置过程中使用的临时方式,切换用户后请重新执行下该命令。

source pgsql-15.6-env.sh
  1. 启动数据库
pg_ctl start -D /pgccc/pgdata/data

输出信息:

waiting for server to start....2024-02-29 22:15:38.584 CST [125692] LOG:  redirecting log output to logging collector process
2024-02-29 22:15:38.584 CST [125692] HINT:  Future log output will appear in directory "pg_log".done
server started
  1. 停止数据库
pg_ctl stop -D /pgccc/pgdata/data
  • pg_ctl stop 命令语法说明:
pg_ctl stop [-D DATADIR] [-m SHUTDOWN-MODE]

参数说明:-m 是指数据库的停止方式,可选 3 种方式:

  • smart,待所有连接终止后关闭数据库。

  • fast,快速断开连接并关闭数据库。

  • immediate,立刻关闭数据库,下次启动数据库需要进行恢复。

如果不指定 -m,则默认使用 fast 方式关闭数据库。

  1. 重启数据库
pg_ctl restart -D /pgccc/pgdata/data
  1. 查看数据库运行状态
  • 方法一:pg_ctl status 命令查看
pg_ctl status -D /pgccc/pgdata/data

输出信息:

pg_ctl: server is running (PID: 125692)
/pgccc/pgdata/bin/postgres "-D" "/pgccc/pgdata/data"
  • 方法二:查看 postgres 进程信息
ps -ef | grep postgres
# (推荐)使用下面方式可以清晰看出层级结构
ps -axjf | grep postgres # 查看指定进程相关信息
lsof -p pid

说明:linux 系统中 lsof 命令加 -p 是指定进程,不加 -p 的是线程。

  • 方法三:查看数据库状态
pg_isready -p 5432
  • 方法四:判断监听端口
# 安装 net-tools
apt install net-tools
# 监听端口
netstat -nutlp | grep 5432

使用 psql 登录数据库

psql 是一个客户端命令工具,可以对数据库实例执行相关操作。

说明:psql 连接数据库,不指定的情况下,默认连接 5452 端口,且使用当前用户查找同名 DB 实例。

  1. 登录方式一:
psql postgresql://postgres:pg123@172.17.0.3:5432/postgres

参数说明:

  • postgresql,协议名称。

  • postgres,数据库用户名。

  • pg123,用户密码。

  • 172.17.0.3,数据库 IP 地址。

  • 5432,数据库实例监听端口。

  • postgres,需要访问的数据库名称。

  1. 登录方式二:
psql -U postgres -h 172.17.0.3 -p 5432 -d postgres

参数说明:

  • -Upostgresql 用户名。

  • -h,数据库 IP 地址。

  • -p,数据库实例监听端口。

  • -d,需要访问的数据库名称。

  1. 登录方式三:使用 psql 直接连接数据库,需要通过设置 postgres 用户的环境变量(env)来实现。
psql

参考:PostgreSQL psql两种登录方式_postgresql登录-CSDN博客

  • 查看 pgsql 更多帮助信息:
psql --help

查看数据库版本信息

  • 登录数据库后,查看数据库(服务端)版本信息:
[postgres@euler /]$ psql -U postgres -p 5432 -d postgres
psql (15.6)
Type "help" for help.postgres=# select version();version                                    
------------------------------------------------------------------------------PostgreSQL 15.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 10.3.1, 64-bit
(1 row)postgres=# SHOW server_version;server_version 
----------------15.6
(1 row)postgres=# SHOW server_version_num;server_version_num 
--------------------150006
(1 row)
  • 推出 psql 查看数据库(服务端)版本信息:
postgres-# \q
[postgres@euler /]$ postgres --version
postgres (PostgreSQL) 15.6
  • 查看数据库客户端工具版本信息
psql --version

注意:psql --version 返回的是 psql 工具的版本,而不是服务器版本。

更多(postgres/pg_ctl)命令说明

说明:pg_ctl 命令本质上是包装了 postgres 的命令操作,推荐使用 pg_ctl 命令。

  • pg_ctl --help
[postgres@euler /]$ pg_ctl --help
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.Usage:pg_ctl init[db]   [-D DATADIR] [-s] [-o OPTIONS]pg_ctl start      [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s][-o OPTIONS] [-p PATH] [-c]pg_ctl stop       [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]pg_ctl restart    [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s][-o OPTIONS] [-c]pg_ctl reload     [-D DATADIR] [-s]pg_ctl status     [-D DATADIR]pg_ctl promote    [-D DATADIR] [-W] [-t SECS] [-s]pg_ctl logrotate  [-D DATADIR] [-s]pg_ctl kill       SIGNALNAME PIDCommon options:-D, --pgdata=DATADIR   location of the database storage area-s, --silent           only print errors, no informational messages-t, --timeout=SECS     seconds to wait when using -w option-V, --version          output version information, then exit-w, --wait             wait until operation completes (default)-W, --no-wait          do not wait until operation completes-?, --help             show this help, then exit
If the -D option is omitted, the environment variable PGDATA is used.Options for start or restart:-c, --core-files       allow postgres to produce core files-l, --log=FILENAME     write (or append) server log to FILENAME-o, --options=OPTIONS  command line options to pass to postgres(PostgreSQL server executable) or initdb-p PATH-TO-POSTGRES    normally not necessaryOptions for stop or restart:-m, --mode=MODE        MODE can be "smart", "fast", or "immediate"Shutdown modes are:smart       quit after all clients have disconnectedfast        quit directly, with proper shutdown (default)immediate   quit without complete shutdown; will lead to recovery on restartAllowed signal names for kill:ABRT HUP INT KILL QUIT TERM USR1 USR2Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
  • postgres --help
[postgres@euler /]$ postgres --help
postgres is the PostgreSQL server.Usage:postgres [OPTION]...Options:-B NBUFFERS        number of shared buffers-c NAME=VALUE      set run-time parameter-C NAME            print value of run-time parameter, then exit-d 1-5             debugging level-D DATADIR         database directory-e                 use European date input format (DMY)-F                 turn fsync off-h HOSTNAME        host name or IP address to listen on-i                 enable TCP/IP connections-k DIRECTORY       Unix-domain socket location-l                 enable SSL connections-N MAX-CONNECT     maximum number of allowed connections-p PORT            port number to listen on-s                 show statistics after each query-S WORK-MEM        set amount of memory for sorts (in kB)-V, --version      output version information, then exit--NAME=VALUE       set run-time parameter--describe-config  describe configuration parameters, then exit-?, --help         show this help, then exitDeveloper options:-f s|i|o|b|t|n|m|h forbid use of some plan types-n                 do not reinitialize shared memory after abnormal exit-O                 allow system table structure changes-P                 disable system indexes-t pa|pl|ex        show timings after each query-T                 send SIGSTOP to all backend processes if one dies-W NUM             wait NUM seconds to allow attach from a debuggerOptions for single-user mode:--single           selects single-user mode (must be first argument)DBNAME             database name (defaults to user name)-d 0-5             override debugging level-E                 echo statement before execution-j                 do not use newline as interactive query delimiter-r FILENAME        send stdout and stderr to given fileOptions for bootstrapping mode:--boot             selects bootstrapping mode (must be first argument)--check            selects check mode (must be first argument)DBNAME             database name (mandatory argument in bootstrapping mode)-r FILENAME        send stdout and stderr to given filePlease read the documentation for the complete list of run-time
configuration settings and how to set them on the command line or in
the configuration file.Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/501022.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

稀疏图带负边的全源最短路Johnson算法

BellmanFord算法 Johnson算法解决的问题 带负权的稀疏图的全源最短路 算法流程 重新设置的每条边的权重都大于或等于0&#xff0c;跑完Djikstra后得到的全源最短路&#xff0c;记得要还原&#xff0c;即&#xff1a;f(u,v) d(u,v) - h[u] h[v] 例题

提升智能客服机器人的语义理解能力:理解用户的语义和意图

智能客服机器人的发展已经成为现代服务业的一大亮点。它们不仅能够提供724小时不间断的服务&#xff0c;而且能够处理大量的用户请求&#xff0c;大大提高了服务效率。然而&#xff0c;尽管智能客服机器人的技术已经取得了显著的进步&#xff0c;但其语义理解能力仍有待提高。为…

StarRocks实战——vivo基于 StarRocks 构建实时大数据平台

目录 前言 一、数据挑战 1.1 时效性挑战&#xff0c;业务分析决策需加速 1.2 访问量挑战&#xff0c;性能与稳定性亟待提高&#xff0c;支撑业务稳定运行 1.3 计算场景挑战&#xff0c;难以满足业务复杂查询需求 1.4. 运维挑战&#xff0c;用户查询体验需优化 二、OLA…

Flink——芒果TV的实时数仓建设实践

目录 一、芒果TV实时数仓建设历程 1.1 阶段一&#xff1a;Storm/Flink JavaSpark SQL 1.2 阶段二&#xff1a;Flink SQLSpark SQL 1.3 阶段三&#xff1a;Flink SQLStarRocks 二、自研Flink实时计算调度平台介绍 2.1 现有痛点 2.2 平台架构设计 三、Flink SQL实时数仓分…

utniy urp shinyssrr插件使用

文章目录 前言步骤1首先在URP的配置文件里添加SSR后处理2 修改RenderingPath为延迟渲染3 启用深度纹理4 为物体添加脚本 插件下载 前言 用来实现屏幕空间反射效果 unity 版本为2021.3.8LTS&#xff0c;低版本的untiy URP的参数设置位置z可能会不同 步骤 1首先在URP的配置文件…

经销商文件分发 怎样兼顾安全和效率?

经销商文件分发是指将文件、资料、产品信息等从制造商或经销商传递给经销商的过程。这一过程对于确保经销商能够获取最新的产品信息、销售策略、市场活动资料等至关重要。 想要管理众多经销商合作伙伴之间的文件传输并提高效率&#xff0c;可以采取以下措施&#xff1a; 1、建…

gpt批量原创文章生成器,不限制内容的生成器

在当今的数字化时代&#xff0c;内容创作是网站持续发展的重要组成部分。然而&#xff0c;对于拥有大量内容需求的网站来说&#xff0c;手动创作文章可能会耗费大量时间和精力。为了解决这一问题&#xff0c;许多GPT&#xff08;生成式预训练模型&#xff09;文章生成软件应运而…

2024第三届中国氢能国际峰会:聚焦清洁能源,共创绿色未来

2024第三届中国氢能国际峰会将于5月16日至17日在上海举行&#xff0c;由ECV International主办。此次峰会旨在搭建一个全球性平台&#xff0c;促进氢能技术的国际交流与合作&#xff0c;推动氢能产业的绿色可持续发展&#xff0c;共同应对全球气候变化和能源安全的挑战。 会议背…

C# Post数据或文件到指定的服务器进行接收

目录 应用场景 实现原理 实现代码 PostAnyWhere类 ashx文件部署 小结 应用场景 不同的接口服务器处理不同的应用&#xff0c;我们会在实际应用中将A服务器的数据提交给B服务器进行数据接收并处理业务。 比如我们想要处理一个OFFICE文件&#xff0c;由用户上传到A服务器…

P-States/C-States/S-States/G-States/D-States

P-States是指处理器的性能状态&#xff0c;可以根据需要调整处理器的工作频率和电压来平衡性能和能效。 S-States是指系统的睡眠状态&#xff0c;可以让系统在空闲时进入低功耗状态以节省能量。 G-States是系统的全局状态&#xff0c;通常用于描述整个系统的运行状态。 C-St…

消息中间件之RocketMQ源码分析(二十四)

事务消息 事务消息机制。 事务消息的发送和处理总结为四个过程: 1.生产者发送事务消息和执行本地事务 2.Broker存储事务消息 3.Broker回查事务消息 4.Broker提交或回滚事务消息 生产者发送事务消息和执行本地事务。 发送过程分为两个阶段: 第一阶段,发送事务消息 第二阶段,发…

Vue——携带参数跳转路由

Vue学习之——跳转路由 前情回顾 当我们进行点击修改时&#xff0c;会进行跳转到修改页面&#xff0c;为了完成回显数据&#xff08;根据对应id查找&#xff09;&#xff0c;我们需要携带对应选择中的id跳转到修改页面&#xff0c;让其进行查找回显 学习useRoute和useRoute…