云原生之使用docker部署Postgresql数据库

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 云原生之使用docker部署Postgresql数据库

一、Postgresql介绍

1.PostgreSQL简介

PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),在灵活的BSD许可证下发行。

2.PostgreSQL的特点

  • 维护者是PostgreSQL Global Development Group,首次发布于1989年6月。
  • 操作系统支持WINDOWS、Linux、UNIX、MAC OS X、BSD。
  • 从基本功能上来看,支持ACID、关联完整性、数据库事务、Unicode多国语言。
  • 表和视图方面,PostgreSQL支持临时表,而物化视图,可以使用PL/pgSQL、PL/Perl、PL/Python或其他过程语言的存储过程和触发器模拟。
  • 索引方面,全面支持R-/R+tree索引、哈希索引、反向索引、部分索引、Expression 索引、GiST、GIN(用来加速全文检索),从8.3版本开始支持位图索引。
  • 其他对象上,支持数据域,支持存储过程、触发器、函数、外部调用、游标7)数据表分区方面,支持4种分区,即范围、哈希、混合、列表。
  • 从事务的支持度上看,对事务的支持与MySQL相比,经历了更为彻底的测试。
  • My ISAM表处理方式方面,MySQL对于无事务的MyISAM表,采用表锁定,1个长时间运行的查询很可能会阻碍对表的更新,而PostgreSQL不存在这样的问题。
  • 从存储过程上看,PostgreSQL支持存储过程。因为存储过程的存在也避免了在网络上大量原始的SQL语句的传输,这样的优势是显而易见的。
  • 用户定义函数的扩展方面,PostgreSQL可以更方便地使用UDF(用户定义函数)进行扩展。

二、检查本地docker环境

1.检查系统版本

[root@docker ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

2.检查docker版本

[root@docker ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:30:24 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:28:33 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.14
  GitCommit:        9ba4b250366a5ddde94bb7c9d1def331423aa323
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

3.检查docker状态

[root@docker ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-01-12 20:05:15 CST; 4 days ago
     Docs: https://docs.docker.com
 Main PID: 5888 (dockerd)
    Tasks: 10
   Memory: 112.9M
   CGroup: /system.slice/docker.service
           └─5888 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

三、下载Postgresql镜像

[root@docker ~]# docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
8740c948ffd4: Pull complete 
c8dbd2beab50: Pull complete 
05d9dc9d0fbd: Pull complete 
ddd89d5ec714: Pull complete 
f98bb9f03867: Pull complete 
0554611e703f: Pull complete 
64e0a8694477: Pull complete 
8b868a753f47: Pull complete 
12ed9aefbab3: Pull complete 
825b08d51ffd: Pull complete 
8f272b487267: Pull complete 
ba2eed7bd2cc: Pull complete 
ff59f63f47d6: Pull complete 
Digest: sha256:6b07fc4fbcf551ea4546093e90cecefc9dc60d7ea8c56a4ace704940b6d6b7a3
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

四、部署Postgresql数据库

1.创建Postgresql容器

docker run -d --name postgres --restart always -e POSTGRES_PASSWORD='admin'   -e POSTGRES_USER='admin' -e ALLOW_IP_RANGE=0.0.0.0/0 -v /data/postgres/data:/var/lib/postgresql -p 55433:5432 -d postgres
–name : 自定义容器名称
-e POSTGRES_PASSWORD: Postgresql数据库密码
-e POSTGRES_USER: Postgresql数据库账号
-e ALLOW_IP_RANGE=0.0.0.0/0,这个表示允许所有ip访问,如果不加,则非本机 ip 访问不了。
-v :   本地文件系统目录:容器内目录
-p:   映射端口,宿主机端口:容器端口

在这里插入图片描述

2.查看Postgresql容器状态

[root@docker ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                         NAMES
de5b3c8570a9   postgres   "docker-entrypoint.s…"   6 minutes ago   Up 6 minutes   0.0.0.0:55433->5432/tcp, :::55433->5432/tcp   postgres

3.查看Postgresql容器日志

查看Postgresql容器日志,检查Postgresql数据库是否正常启动。

[root@docker ~]# docker logs postgres
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.utf8".
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/postgresql/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 ... Etc/UTC
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:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2023-01-17 06:10:55.857 UTC [47] LOG:  starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-01-17 06:10:55.883 UTC [47] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-01-17 06:10:55.962 UTC [50] LOG:  database system was shut down at 2023-01-17 06:10:54 UTC
2023-01-17 06:10:55.988 UTC [47] LOG:  database system is ready to accept connections
 done
server started
CREATE DATABASE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

2023-01-17 06:10:56.314 UTC [47] LOG:  received fast shutdown request
waiting for server to shut down....2023-01-17 06:10:56.349 UTC [47] LOG:  aborting any active transactions
2023-01-17 06:10:56.351 UTC [47] LOG:  background worker "logical replication launcher" (PID 53) exited with exit code 1
2023-01-17 06:10:56.351 UTC [48] LOG:  shutting down
2023-01-17 06:10:56.375 UTC [48] LOG:  checkpoint starting: shutdown immediate
2023-01-17 06:10:56.783 UTC [48] LOG:  checkpoint complete: wrote 918 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.016 s, sync=0.306 s, total=0.433 s; sync files=250, longest=0.253 s, average=0.002 s; distance=4217 kB, estimate=4217 kB
2023-01-17 06:10:56.787 UTC [47] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2023-01-17 06:10:56.896 UTC [1] LOG:  starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-01-17 06:10:56.897 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-01-17 06:10:56.897 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-01-17 06:10:56.948 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-01-17 06:10:57.000 UTC [63] LOG:  database system was shut down at 2023-01-17 06:10:56 UTC
2023-01-17 06:10:57.026 UTC [1] LOG:  database system is ready to accept connections
2023-01-17 06:15:57.100 UTC [61] LOG:  checkpoint starting: time
2023-01-17 06:16:01.354 UTC [61] LOG:  checkpoint complete: wrote 44 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=4.110 s, sync=0.055 s, total=4.254 s; sync files=12, longest=0.029 s, average=0.005 s; distance=252 kB, estimate=252 kB

五、远程连接Postgresql数据库

1.psql客户端连接

[root@docker ~]# psql -h192.168.3.157 -p 55433 -Uadmin -W
Password: 
psql (13.9, server 15.1 (Debian 15.1-1.pgdg110+1))
WARNING: psql major version 13, server major version 15.
         Some psql features might not work.
Type "help" for help.

admin=#

2.查看Postgresql内的所有数据库

admin=# \l
                             List of databases
   Name    | Owner | Encoding |  Collate   |   Ctype    | Access privileges 
-----------+-------+----------+------------+------------+-------------------
 admin     | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres  | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | admin | UTF8     | en_US.utf8 | en_US.utf8 | =c/admin         +
           |       |          |            |            | admin=CTc/admin
 template1 | admin | UTF8     | en_US.utf8 | en_US.utf8 | =c/admin         +
           |       |          |            |            | admin=CTc/admin
(4 rows)

admin=#
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
14天前
|
JavaScript 算法 前端开发
【Docker项目实战】使用Docker部署paopao-ce微社区
【Docker项目实战】使用Docker部署paopao-ce微社区
159 84
【Docker项目实战】使用Docker部署paopao-ce微社区
|
23天前
|
运维 Cloud Native 开发者
Docker:现代化应用开发与部署的神器
Docker:现代化应用开发与部署的神器
166 101
|
5天前
|
存储 Docker Python
docker 部署 sftp
本文介绍SFTP服务的部署与配置,包括users.conf用户配置规则、Docker容器运行命令及上传目录权限说明,重点解析atmoz/sftp镜像的chroot机制与子目录映射,确保用户登录后正确访问/upload目录,并提供Python脚本实现文件上传示例。
43 12
docker 部署 sftp
|
7天前
|
运维 Linux 数据库
基于 Docker 部署 n8n 指南,新手一看就会
本教程详解如何通过 Docker 快速部署开源自动化工具 n8n,适合新手快速上手。内容涵盖官方部署步骤、常见难点及第三方一键部署方案,助你高效搭建自动化工作流平台。
145 6
|
1天前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
13天前
|
前端开发 JavaScript 应用服务中间件
在Docker部署的前端应用中使用动态环境变量
以上步骤展示了如何在 Docker 配置过程中处理并注入环墨遁形成可执行操作流程,并确保最终用户能够无缝地与之交互而无须关心背后复杂性。
54 13
|
16天前
|
存储 Kubernetes 持续交付
为什么Docker容器化改变了开发与部署?
为什么Docker容器化改变了开发与部署?
|
7天前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
|
缓存 NoSQL 关系型数据库
DOCKER03_快速安装docker、数据库mysql、缓存redis(二)
③. 数据库mysql5.7安装 ④. 缓存redis安装
304 0
DOCKER03_快速安装docker、数据库mysql、缓存redis(二)
|
缓存 NoSQL 关系型数据库
DOCKER03_快速安装docker、数据库mysql、缓存redis(一)
①. 快速安装docker ②. 阿里云镜像加速
194 0
DOCKER03_快速安装docker、数据库mysql、缓存redis(一)