Docker 安装 PostgreSQL

1. 启动 PostgreSQL 容器

复制代码
docker run --name ffj-postgres -p 5432:5432 -e POSTGRES_PASSWORD=Cisc0123 -d postgres
  • docker run:启动一个新的容器。
  • --name指定容器名称为 ffj-postgres
  • -p 5432:5432:将主机的 5432 端口映射到容器的 5432 端口。
  • -e POSTGRES_PASSWORD=Cisc0123:设置 PostgreSQL 的 postgres 用户的密码为 Cisc0123
  • -d postgres:后台运行容器,并使用 postgres 镜像。

2. 获取容器的 IP 地址

复制代码
docker inspect -f '{{.NetworkSettings.IPAddress}}' ffj-postgres
  • docker inspect:查看容器的详细信息。
  • -f '{``{.NetworkSettings.IPAddress}}':格式化输出以仅显示 IP 地址。
  • ffj-postgres:指定要查看的容器名称。

这一步的输出会是容器的 IP 地址,例如 172.17.0.2

3. 启动一个新的 CentOS 容器

复制代码
docker run --rm -it --name ffj-centos1 centos
  • docker run:启动一个新的容器。
  • --rm:容器退出时自动删除容器。
  • -it:使容器运行在交互模式并分配一个伪终端。
  • --name ffj-centos1:指定容器名称为 ffj-centos1
  • centos:使用 centos 镜像。

4. 在 CentOS 容器中安装 PostgreSQL 客户端

复制代码
yum install -y postgresql
  • yum install -y:自动确认安装 postgresql

由于centos停止维护,需要更换源 ,容器没有vim命令,使用echo

复制代码
echo "[BaseOS]
name=CentOS-\$releasever - Base
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=CentOS-\$releasever - AppStream
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=CentOS-\$releasever - Extras
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/extras/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
" > /etc/yum.repos.d/CentOS-Base.repo

yum clean all && yum makecache

5. 通过 psql 客户端连接到 PostgreSQL 容器

复制代码
psql -U postgres -d postgres -h 172.17.0.3
  • psql:PostgreSQL 命令行客户端。
  • -U postgres:使用 postgres 用户连接。
  • -d postgres:连接到 postgres 数据库。
  • -h 172.17.0.3:指定 PostgreSQL 服务器的 IP 地址。

6. 在 PostgreSQL 中执行 SQL 操作

以下是连接到 PostgreSQL 后的 SQL 操作:

  1. 切换到 postgres 数据库

    \c postgres

  2. 创建表 test1

    create table test1(t1 int, t2 varchar(40));

  3. 插入数据

    insert into test1(t1, t2) values (11, 'welcome to fengfujiang');
    insert into test1(t1, t2) values (12, 'welcome to python');

  4. 查询数据

    select * from test1;

相关推荐
Hadoop_Liang17 小时前
使用Kubernetes Gateway API实现域名访问应用
容器·kubernetes·gateway
java_cj20 小时前
深入kubectl create源码:从YAML到Pod的完整链路拆解
运维·云原生·容器·kubernetes
源图客1 天前
【AI向量数据库】Weaviate介绍与部署
运维·docker·容器
一勺菠萝丶1 天前
宝塔 Docker 安装 Ubuntu 及启动失败解决流程
ubuntu·docker·eureka
码云骑士1 天前
28-Docker部署Django(下)-docker-compose编排与静态文件处理
docker·容器·django
至乐活着1 天前
Docker Compose多服务编排实战:从零搭建Node.js+MySQL+Redis全栈应用
docker·微服务·devops·容器编排·compose
木雷坞1 天前
Firecrawl Docker Compose 自托管排查:镜像、Redis、队列和 Playwright
redis·docker·容器·firecrawl
睡不醒男孩0308231 天前
PostgreSQL 数据库运维转型:从传统模式到 CLup 平台的 25 个核心 FAQ
运维·数据库·postgresql
whyfail1 天前
Colima:把 Docker Desktop 从 Mac 上“瘦身”的那把刀
macos·docker·容器
人工智能培训1 天前
数字孪生的未来发展方向探析
gpt·深度学习·机器学习·容器·知识图谱