当前位置: 代码网 > it编程>数据库>MsSqlserver > Docker安装PostgreSQL数据库的详细步骤

Docker安装PostgreSQL数据库的详细步骤

2024年10月30日 MsSqlserver 我要评论
1. 启动 postgresql 容器docker run --name ffj-postgres -p 5432:5432 -e postgres_password=cisc0123 -d post

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 操作:

  • 切换到 postgres 数据库
\c postgres
  • 创建表 test1
create table test1(t1 int, t2 varchar(40));
  • 插入数据
insert into test1(t1, t2) values (11, 'welcome to fengfujiang');
insert into test1(t1, t2) values (12, 'welcome to python');
  • 查询数据
select * from test1;

总结 

到此这篇关于docker安装postgresql数据库的文章就介绍到这了,更多相关docker 安装 postgresql内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com