Docker安裝PostgreSQL數(shù)據(jù)庫的詳細(xì)步驟
1. 啟動(dòng) PostgreSQL 容器
docker run --name ffj-postgres -p 5432:5432 -e POSTGRES_PASSWORD=Cisc0123 -d postgres
docker run:啟動(dòng)一個(gè)新的容器。--name指定容器名稱為 ffj-postgres。-p 5432:5432:將主機(jī)的 5432 端口映射到容器的 5432 端口。-e POSTGRES_PASSWORD=Cisc0123:設(shè)置 PostgreSQL 的postgres用戶的密碼為Cisc0123。-d postgres:后臺(tái)運(yùn)行容器,并使用postgres鏡像。
2. 獲取容器的 IP 地址
docker inspect -f '{{.NetworkSettings.IPAddress}}' ffj-postgresdocker inspect:查看容器的詳細(xì)信息。-f '{{.NetworkSettings.IPAddress}}':格式化輸出以僅顯示 IP 地址。ffj-postgres:指定要查看的容器名稱。
這一步的輸出會(huì)是容器的 IP 地址,例如 172.17.0.2。
3. 啟動(dòng)一個(gè)新的 CentOS 容器
docker run --rm -it --name ffj-centos1 centos
docker run:啟動(dòng)一個(gè)新的容器。--rm:容器退出時(shí)自動(dòng)刪除容器。-it:使容器運(yùn)行在交互模式并分配一個(gè)偽終端。--name ffj-centos1:指定容器名稱為 ffj-centos1。centos:使用centos鏡像。
4. 在 CentOS 容器中安裝 PostgreSQL 客戶端
yum install -y postgresql
yum install -y:自動(dòng)確認(rèn)安裝postgresql包
由于centos停止維護(hù),需要更換源 ,容器沒有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數(shù)據(jù)庫。-h 172.17.0.3:指定 PostgreSQL 服務(wù)器的 IP 地址。
6. 在 PostgreSQL 中執(zhí)行 SQL 操作
以下是連接到 PostgreSQL 后的 SQL 操作:
- 切換到
postgres數(shù)據(jù)庫
\c postgres
- 創(chuàng)建表
test1
create table test1(t1 int, t2 varchar(40));
- 插入數(shù)據(jù)
insert into test1(t1, t2) values (11, 'welcome to fengfujiang'); insert into test1(t1, t2) values (12, 'welcome to python');
- 查詢數(shù)據(jù)
select * from test1;
總結(jié)
到此這篇關(guān)于Docker安裝PostgreSQL數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Docker 安裝 PostgreSQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在 Docker 中安裝和運(yùn)行 PostgreSQL的詳細(xì)步驟
- docker安裝Postgresql數(shù)據(jù)庫及基本操作
- Docker Compose安裝部署PostgreSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- Docker如何安裝PostgreSQL
- docker如何安裝帶postgis插件的postgresql數(shù)據(jù)庫
- docker安裝postgresql的圖文教程
- docker安裝并持久化postgresql數(shù)據(jù)庫的操作步驟
- Docker27.3.1中安裝 PostgreSQL16的實(shí)現(xiàn)步驟
相關(guān)文章
PostgreSQL如何查詢表結(jié)構(gòu)和索引信息
文章介紹了在PostgreSQL中查詢表結(jié)構(gòu)和索引信息的幾種方法,包括使用`\d`元命令、系統(tǒng)數(shù)據(jù)字典查詢以及使用可視化工具DBeaver2024-12-12
PostgreSQL自動(dòng)更新時(shí)間戳實(shí)例代碼
最近有這么一個(gè)工程,需要使用postgresql數(shù)據(jù)庫,在數(shù)據(jù)庫中的好幾個(gè)表中都需要時(shí)間戳這個(gè)字段,這篇文章主要給大家介紹了關(guān)于PostgreSQL自動(dòng)更新時(shí)間戳的相關(guān)資料,需要的朋友可以參考下2021-11-11
PostgreSQL分區(qū)表(partitioning)應(yīng)用實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了PostgreSQL分區(qū)表(partitioning)應(yīng)用實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Debian中PostgreSQL數(shù)據(jù)庫安裝配置實(shí)例
這篇文章主要介紹了Debian中PostgreSQL數(shù)據(jù)庫安裝配置實(shí)例,一個(gè)簡明教程,需要的朋友可以參考下2014-06-06
詳解如何在PostgreSQL中使用JSON數(shù)據(jù)類型
JSON(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,它采用鍵值對的形式來表示數(shù)據(jù),支持多種數(shù)據(jù)類型,本文給大家介紹了如何在PostgreSQL中使用JSON數(shù)據(jù)類型,需要的朋友可以參考下2024-03-03
PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)
這篇文章主要介紹了PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
postgresql 實(shí)現(xiàn)將字段為空的值替換為指定值
這篇文章主要介紹了postgresql 實(shí)現(xiàn)將字段為空的值替換為指定值,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫
這篇文章主要介紹了如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02

