PostgreSQL 安装部署

news2024/10/2 14:38:00

文章目录

    • 一、PostgreSQL部署方式
      • 1.Yum方式部署
      • 2.RPM方式部署
      • 3.源码方式部署
      • 4.二进制方式部署
      • 5.Docker方式部署
    • 二、PostgreSQL部署
      • 1.Yum方式部署
        • 1.1.部署数据库
        • 1.2.连接数据库
      • 2.RPM方式部署
        • 2.1.部署数据库
        • 2.2.连接数据库
      • 3.源码方式部署
        • 3.1.准备工作
        • 3.2.编译安装
        • 3.3.配置数据库
        • 3.4.连接数据库
      • 4.二进制方式部署
        • 4.1.准备工作
        • 4.2.配置数据库
        • 4.3.连接数据库
      • 5.Docker方式部署
        • 5.1.部署数据库

  • 开源中间件
# PostgreSQL

https://iothub.org.cn/docs/middleware/
https://iothub.org.cn/docs/middleware/postgresql/postgres-deploy/

一、PostgreSQL部署方式

1.Yum方式部署

# 下载安装包,本文以二进制包方式安装

# 下载地址:
https://www.postgresql.org/download/

# CentOS安装参考
https://www.postgresql.org/download/linux/redhat/

在这里插入图片描述
在这里插入图片描述

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
 
sudo yum install -y postgresql11-server
 
sudo /usr/pgsql-11/bin/postgresql-11-setup initdb
 
sudo systemctl enable postgresql-11
 
sudo systemctl start postgresql-11

2.RPM方式部署

# 下载地址: 
https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/
 
 
#或者直接执行wget下载
wget https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.2-x86_64/postgresql11-11.4-1PGDG.rhel7.x86_64.rpm
 
wget https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.2-x86_64/postgresql11-libs-11.4-1PGDG.rhel7.x86_64.rpm
 
wget https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.2-x86_64/postgresql11-server-11.4-1PGDG.rhel7.x86_64.rpm

3.源码方式部署

# 获取官方源码包
准备装11.7版本,选择其他版本进入下面的第一个地址后退选择目的版本即可
 
# 安装官方教学
https://www.postgresql.org/docs/11/install-getsource.html
 
# 源码包
https://www.postgresql.org/ftp/source/
 
# 11.7:连接可能随着官方的更新而失效
https://ftp.postgresql.org/pub/source/v11.7/postgresql-11.7.tar.gz
 
https://www.postgresql.org/ftp/source/ 
https://www.postgresql.org/docs/11/install-getsource.html 
https://www.postgresql.org/download/ 

4.二进制方式部署

# 下载二进制包
pgsql有很多类型的包,对于不同linux发行版都有对应的编译好的包,安装很方便,另外如果对于通用的linux平台可以编译源码安装或者安装官方编译好的二进制包,源码包的安装仅仅比二进制安装多出一个编译步骤,其余的都一样,所以这里使用安装方式是安装编译好的二进制包


# pgsql官网地址
https://www.postgresql.org/
进入后点击download就来到下载页,这里点击Linux下面的Other Linux选项,然后点击下方的tar.gz archive下载二进制归档

在这里插入图片描述

然后就来到最终的pgsql下载页了,地址为:
https://www.enterprisedb.com/download-postgresql-binaries
 

https://www.postgresql.org/
https://www.postgresql.org/download/
https://www.enterprisedb.com/download-postgresql-binaries 
 
注意:官网没有11以后的linux版本

5.Docker方式部署

https://hub.docker.com/_/postgres


$ docker run -d \
	--name some-postgres \
	-e POSTGRES_PASSWORD=mysecretpassword \
	-e PGDATA=/var/lib/postgresql/data/pgdata \
	-v /custom/mount:/var/lib/postgresql/data \
	postgres

二、PostgreSQL部署

1.Yum方式部署

1.1.部署数据库
1.安装yum源
# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
 
2.安装postgresql
# yum install -y postgresql11-server
 
3.数据库初始化
# /usr/pgsql-11/bin/postgresql-11-setup initdb
 
4.修改配置文件
# cd /var/lib/pgsql/11/data/

在这里插入图片描述

# 修改配置文件
vim /var/lib/pgsql/11/data/postgresql.conf
 
# 修改为如下:
listen_addresses = '*'
port = 5432 

在这里插入图片描述

# 修改配置文件
vim /var/lib/pgsql/11/data/pg_hba.conf
 
# 修改为如下:
host    all             all             0.0.0.0/0               md5
 
# 或
host    all             all             all                          md5

在这里插入图片描述

# 说明:
TYPE:pg的连接方式,local:本地unix套接字,host:tcp/ip连接
DATABASE:指定数据库
USER:指定数据库用户
ADDRESS:ip地址,可以定义某台主机或某个网段,32代表检查整个ip地址,相当于固定的ip,24代表只检查前三位,最后一位是0~255之间的任何一个
METHOD:认证方式,常用的有ident,md5,password,trust,reject。
    md5是常用的密码认证方式。
    password是以明文密码传送给数据库,建议不要在生产环境中使用。
               trust是只要知道数据库用户名就能登录,建议不要在生产环境中使用。
               reject是拒绝认证。
 
 
5.postgresql 安装目录授权 
# chown postgres:root -R /usr/pgsql-11/ 
 
6.启动服务 
# systemctl start postgresql-11 
 
# 服务自启动
# systemctl enable postgresql-11

#查看端口
# netstat -lntp
# netstat -nat

在这里插入图片描述

1.2.连接数据库
1.切换用户,设置数据库密码 
 
# su - postgres
$ psql -U postgres
# ALTER USER postgres with encrypted password 'postgres';

在这里插入图片描述

退出: \q
列出所有库 \l
列出所有用户 \du
列出库下所有表\d

在这里插入图片描述

2.RPM方式部署

2.1.部署数据库
1.下载RPM包
下载地址: https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/
 
#或者直接执行wget下载 
wget https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.2-x86_64/postgresql11-11.4-1PGDG.rhel7.x86_64.rpm
 
wget https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.2-x86_64/postgresql11-libs-11.4-1PGDG.rhel7.x86_64.rpm
 
wget https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.2-x86_64/postgresql11-server-11.4-1PGDG.rhel7.x86_64.rpm
 
 
2.安装
先安装相应依赖
# yum install -y libicu systemd-sysv 
 
安装rpm包,需要按照这个顺序安装,卸载就反序卸载
# rpm -ivh postgresql11-libs-11.4-1PGDG.rhel7.x86_64.rpm
# rpm -ivh postgresql11-11.4-1PGDG.rhel7.x86_64.rpm
# rpm -ivh postgresql11-server-11.4-1PGDG.rhel7.x86_64.rpm
 
卸载
rpm -e postgresql11-server-11.4-1PGDG.rhel7.x86_64
rpm -e postgresql11-11.4-1PGDG.rhel7.x86_64
rpm -e postgresql11-libs-11.4-1PGDG.rhel7.x86_64
rm -rf /usr/pgsql-11/
rm -rf /var/lib/pgsql/ 
 
3.数据库初始化
# /usr/pgsql-11/bin/postgresql-11-setup initdb 
 
4.修改配置文件
# cd /var/lib/pgsql/11/data/

在这里插入图片描述

# 修改配置文件
vim /var/lib/pgsql/11/data/postgresql.conf
 
# 修改为如下:
listen_addresses = '*'
port = 5432 

在这里插入图片描述

# 修改配置文件
vim /var/lib/pgsql/11/data/pg_hba.conf
 
# 修改为如下:
host    all             all             0.0.0.0/0               md5
 
# 或
host    all             all             all                          md5

在这里插入图片描述

# 说明:
TYPE:pg的连接方式,local:本地unix套接字,host:tcp/ip连接
DATABASE:指定数据库
USER:指定数据库用户
ADDRESS:ip地址,可以定义某台主机或某个网段,32代表检查整个ip地址,相当于固定的ip,24代表只检查前三位,最后一位是0~255之间的任何一个
METHOD:认证方式,常用的有ident,md5,password,trust,reject。
    md5是常用的密码认证方式。
    password是以明文密码传送给数据库,建议不要在生产环境中使用。
               trust是只要知道数据库用户名就能登录,建议不要在生产环境中使用。
               reject是拒绝认证。
 
 
5.postgresql 安装目录授权  
# chown postgres:root -R /usr/pgsql-11/ 
 
6.启动服务 
# systemctl start postgresql-11 
 
服务自启动
# systemctl enable postgresql-11

查看端口
# netstat -lntp
# netstat -nat

在这里插入图片描述

2.2.连接数据库
1.切换用户,设置数据库密码 
 
# su - postgres
$ psql -U postgres
# ALTER USER postgres with encrypted password 'postgres';

在这里插入图片描述

退出: \q
列出所有库 \l
列出所有用户 \du
列出库下所有表\d

在这里插入图片描述

3.源码方式部署

3.1.准备工作
1.1.安装gcc编辑器
# gcc --version 
 
1.2.安装make
# make --version
 
1.3.安装readline
# yum install readline
# yum -y install -y readline-devel
 
1.4.安装zlib-devel
# yum install zlib
# yum install zlib-devel
 
1.5.下载postgresql源码
postgresql的官方网站下载:
https://www.postgresql.org/
3.2.编译安装
2.1.解压下载的压缩包
# cd /root
# tar -xzvf postgresql-11.0.tar.gz
 
创建postgresql的安装目录
# mkdir -p /home/app/postgresql/data 
 
2.2.生成makefile
# cd postgresql-11.0
# ./configure --prefix=/home/app/postgresql
 
2.3.编译安装
# make && make install
  
2.4.安装工具集
# cd /home/software/postgresql-11.0/contrib
# make && make install
3.3.配置数据库
3.1.创建postgres用户:
# groupadd postgres
# useradd -g postgres postgres
 
3.2.修改data目录的用户为postgres
chown -R postgres:postgres /home/app/postgresql/data
 
3.3.修改环境变量
# su postgres
$ vim /home/postgres/.bash_profile
 
添加环境变量:
    export PGHOME=/home/app/postgresql
    export PGDATA=/home/app/postgresql/data
    export PATH=$PGHOME/bin:$PATH
    export MANPATH=$PGHOME/share/man:$MANPATH
    export LANG=en_US.utf8
    export DATE=`date +"%Y-%m-%d %H:%M:%S"`
    export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
    alias rm='rm  -i'
    alias ll='ls -lh'
然后使环境变量立即生效,否则initdb命令会找不到:
 
$ source /home/postgres/.bash_profile
 
3.4.初始化数据库
$ initdb -D /home/app/postgresql/data/
Success. You can now start the database server using:
 
    pg_ctl -D /home/app/postgresql/data/ -l logfile start
 
3.5.修改配置文件
# cd /home/app/postgresql/data/

在这里插入图片描述

修改配置文件
vim /home/app/postgresql/data/postgresql.conf
 
# 修改为如下:
listen_addresses = '*'
port = 5432 

在这里插入图片描述

# 修改配置文件
vim /home/app/postgresql/data/pg_hba.conf
 
# 修改为如下:
host    all             all             0.0.0.0/0               md5
 
# 或
host    all             all             all                          md5

在这里插入图片描述

3.6.启动数据库
$ su postgres
$ cd /home/postgres
$ pg_ctl -D /home/app/postgresql/data/ -l logfile start
 
    cd /home/postgres  //logfile需要在postgres的用户目录下创建
    pg_ctl -D /home/app/postgresql/data/ -l logfile start
 
 
查看端口
# netstat -lntp
# netstat -nat

在这里插入图片描述

3.7.添加postgresql至服务(未验证)
    cd /home/software/postgresql-11.4/contrib/start-scripts
    chmod a+x linux.
    cp linux /etc/init.d/postgresql
此时就可以使用 /etc/init.d/postgresql stop 来停止postgresql
也可以使用:service postgresql start 来启动postgresql
修改postgresql脚本中prefix和PGDATA的内容
chkconfig --add postgresql //设置服务开机启动
3.4.连接数据库

参考 1.2.连接数据库

4.二进制方式部署

4.1.准备工作
1.下载软件
# wget https://get.enterprisedb.com/postgresql/postgresql-10.12-1-linux-x64-binaries.tar.gz
 
2.创建postgres用户
# groupadd postgres
# useradd -g postgres postgres
 
3.创建postgresql的安装目录
# mkdir -p /home/app
 
进入下载目录解压
# tar -xzvf postgresql-10.12-1-linux-x64-binaries.tar.gz -C /home/app
 
创建data目录:
# mkdir -p /home/app/pgsql/data
# mkdir -p /home/app/pgsql/log
 
授权:
# cd /home/app/pgsql
# chown -R postgres.postgres pgsql
4.2.配置数据库
1.初始化数据库
切换用户 postgres,并执行初始化操作
# su postgres
 
# cd /home/app/pgsql/bin
 
$ ./initdb -E utf8 -D /home/app/pgsql/data
Success. You can now start the database server using:
 
    ./pg_ctl -D /home/app/pgsql/data -l logfile start
 
 
2.配置环境变量
~/.bash_profile 添加如下内容
注:这个文件目录是在当前用户(postgres)的根目录下,即/home/{User}/
 
[postgres@iZ2ze72jggg737pb9vp1g6Z data]$ vim ~/.bash_profile
 
PATH=/home/app/pgsql/bin:$PATH
export PATH
 
配置文件生效
$ source ~/.bash_profile
 
修改后的配置文件
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
 
# User specific environment and startup programs
 
PATH=$PATH:$HOME/.local/bin:$HOME/bin
 
PATH=/home/app/pgsql/bin:$PATH
 
export PATH
 
 
3.修改配置文件
# cd /home/app/postgresql/data/

在这里插入图片描述

# 修改配置文件
vim /home/app/postgresql/data/postgresql.conf
 
# 修改为如下:
listen_addresses = '*'
port = 5432 

在这里插入图片描述

修改配置文件
vim /home/app/postgresql/data/pg_hba.conf
 
# 修改为如下:
host    all             all             0.0.0.0/0               md5
 
或
host    all             all             all                          md5

在这里插入图片描述

4.启动数据库
$ cd /home/app/pgsql/bin
$ ./pg_ctl -D /home/app/pgsql/data -l logfile start
 
查看端口
# netstat -lntp
# netstat -nat
4.3.连接数据库

参考 1.2.连接数据库

5.Docker方式部署

5.1.部署数据库
1.创建目录
# mkdir -p /data/psql
 
2.运行容器
docker run -d --network host --name pg12 --restart=always \
-e LANG="C.UTF-8" \
-e 'TZ=Asia/Shanghai' \
-e "POSTGRES_DB=postgres" \
-e "POSTGRES_USER=postgres" \
-e "POSTGRES_PASSWORD=postgres" \
-v /data/psql:/var/lib/postgresql/data \
postgres:12
 
3.进入容器
# docker exec -it pg12 /bin/sh
 
切换用户
# su - postgres
$ psql 
# \l
  • 开源中间件
# PostgreSQL

https://iothub.org.cn/docs/middleware/
https://iothub.org.cn/docs/middleware/postgresql/postgres-deploy/

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[LeetCode][LCR151]彩灯装饰记录 III——队列

题目 LCR 151. 彩灯装饰记录 III 一棵圣诞树记作根节点为 root 的二叉树,节点值为该位置装饰彩灯的颜色编号。请按照如下规则记录彩灯装饰结果: 第一层按照从左到右的顺序记录除第一层外每一层的记录顺序均与上一层相反。即第一层为从左到右&#xff0c…

Spring AOP底层原理

目录 代理模式 静态代理 动态代理 1. JDK动态代理 创建⼀个代理对象并使用 2. CGLIB动态代理 SpringAOP底层原理面试 代理模式 Spring AOP是基于动态代理模式来实现的 代理模式:静态代理模式动态代理模式 代理模式, 也叫委托模式。 定义:为其…

Mysql - is marked as crashed and should be repaired

概述 上周发生了一个Mysql报错的问题,今天有时间整理一下产生的原因和来龙去脉,Mysql的版本是5.5,发生错误的表存储引擎都是MyISAM,产生的报错信息是Table xxxxxx is marked as crashed and should be repaired。 定位问题 产生的后果是Nginx服务没有…

Util工具类功能设计与类设计(http模块一)

目录 类功能 类定义 类实现 编译测试 Split分割字符串测试 ReadFile读取测试 WriteFile写入测试 UrlEncode编码测试 UrlDecode编码测试 StatuDesc状态码信息获取测试 ExtMime后缀名获取文件mime测试 IsDirectory&IsRegular测试 VaildPath请求路径有效性判断测…

基于Springboot的招生宣传管理系统(有报告)。Javaee项目,springboot项目。

演示视频: 基于Springboot的招生宣传管理系统(有报告)。Javaee项目,springboot项目。 项目介绍: 采用M(model)V(view)C(controller)三层体系结构…

基于springboot的医院信息管理系统(程序+代码+文档)

** 🍅点赞收藏关注 → 私信领取本源代码、数据库🍅 本人在Java毕业设计领域有多年的经验,陆续会更新更多优质的Java实战项目,希望你能有所收获,少走一些弯路。🍅关注我不迷路🍅** 一、研究背景…

智慧公厕方案_智慧公厕解决方案_智慧公厕整体解决方案

一、什么是智慧公厕? 在现代城市化进程中,公共厕所是不可或缺的基础设施之一。然而,传统的公厕管理模式已经无法满足市民对高效、便捷厕所服务的需求。为了实现公共厕所的信息化管理,智慧公厕整体解决方案应运而生。智慧公厕具体…

【MySQL】表的增删改查——MySQL基本查询、数据库表的创建、表的读取、表的更新、表的删除

文章目录 MySQL表的增删查改1. Create(创建)1.1 单行插入1.2 多行插入1.3 替换 2. Retrieve(读取)2.1 select查看2.2 where条件2.3 结果排序2.4 筛选分页结果 3. Update(更新)3.1 更新单个数据3.2 更新多个…

边缘计算相关实验01

1 ChatGLM2-6B模型本地部署 部署的轻量级的模型,硬件配置不够,无法跑起来 2 车辆识别 2.1 车牌识别 开源代码 ​​https://gitcode.net/mirrors/we0091234/chinese_license_plate_detection_recognition?utm_sourcecsdn_github_accelerator​​ 识…

STM32基础--使用寄存器点亮流水灯

GPIO 简介 GPIO 是通用输入输出端口的简称,简单来说就是 STM32 可控制的引脚,STM32 芯片的 GPIO 引脚与外部设备连接起来,从而实现与外部通讯、控制以及数据采集的功能。STM32 芯片的 GPIO被分成很多组,每组有 16 个引脚&#xf…

Pulsar 社区周报 | No.2024.03.08 Pulsar-Spark Connector 助力实时计算

关于 Apache Pulsar Apache Pulsar 是 Apache 软件基金会顶级项目,是下一代云原生分布式消息流平台,集消息、存储、轻量化函数式计算为一体,采用计算与存储分离架构设计,支持多租户、持久化存储、多机房跨区域数据复制&#xff0c…

Linux常用操作命令-防火墙常用操作

一、防火墙常用操作 1、查看防火墙的状态; systemctl status firewalld 2、启动防火墙 systemctl start firewalld.service 3、开启某个端口,如8081端口,输入命令 firewall-cmd --zonepublic --add-port8088/tcp --permanent 4、删除某个…

C语言实战——扫雷游戏

目录 1. 扫雷游戏分析和设计2.扫雷游戏的代码实现 1. 扫雷游戏分析和设计 1.1扫雷游戏的功能说明 使用控制台实现经典的扫雷游戏游戏可以通过菜单实现继续玩或者退出游戏扫雷的棋盘是9*9的格子默认随机布置10个雷可以排查雷 如果位置不是雷,就显示周围有几个雷 如果…

动态规划课堂4-----子数组系列

目录 引入: 例题1:最大子数组和 例题2:环形子数组的最大和 例题3:乘积最大子数组 例题4:乘积为正数的最长子数组 总结: 结语: 引入: 在动态规划(DP)子…

【新书推荐】19.1 DOS程序段前缀PSP

本节内容:介绍DOS程序段前缀及其应用。 ■程序段前缀PSP:DOS系统加载程序时,在程序段前设置一个具有256字节的信息区称为程序段前缀PSP。 ■终止程序的另一途径:利用程序段前缀PSP偏移地址0处的“INT 20H”终止程序。示例代码t19-…

第三百九十二回

文章目录 1. 概念介绍2. 方法与细节2.1 实现方法2.2 具体细节 3. 示例代码4. 内容总结 我们在上一章回中介绍了"如何混合选择多个图片和视频文件"相关的内容,本章回中将介绍如何通过相机获取图片文件.闲话休提,让我们一起Talk Flutter吧。 1. …

NoSQL--3.MongoDB配置(Linux版)

目录 2.2 Linux环境下操作 2.2.1 传输MongoDB压缩包到虚拟机: 2.2.2 启动MongoDB服务: 2.2 Linux环境下操作 2.2.1 传输MongoDB压缩包到虚拟机: (笔者使用XShell传输) 如果不想放在如图的路径,删除操作…

【Python使用】python高级进阶知识md总结第2篇:HTTP 请求报文,HTTP响应报文【附代码文档】

python高级进阶全知识知识笔记总结完整教程(附代码资料)主要内容讲述:操作系统,虚拟机软件,Ubuntu操作系统,Linux内核及发行版,查看目录命令,切换目录命令,绝对路径和相对…

(001)UV 的使用以及导出

文章目录 UV窗口导出模型的主要事项导出时材质的兼容问题unity贴图导出导出FBX附录 UV窗口 1.uv主要的工作区域: 2.在做 uv 和贴图之前,最好先应用下物体的缩放、旋转。 导出模型的主要事项 1.将原点设置到物体模型的底部: 2.应用修改器的…

【SpringCloud】微服务重点解析

微服务重点解析 1. Spring Cloud 组件有哪些? 2. 服务注册和发现是什么意思?Spring Cloud 如何实现服务注册和发现的? 如果写过微服务项目,可以说做过的哪个微服务项目,使用了哪个注册中心,常见的有 eurek…