c9d667090991886446ef1c1b928b9c5f9543a256
PostgreSQL.md
| ... | ... | @@ -4,6 +4,138 @@ PostgreSQL版本:16.6 |
| 4 | 4 | |
| 5 | 5 | 下载地址: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads |
| 6 | 6 | |
| 7 | +## 第 1 节. PG本地和Docker安装步骤 |
|
| 8 | + |
|
| 9 | +### 一、本地安装PostgreSQL |
|
| 10 | + |
|
| 11 | +以下是在本地 Windows 系统上安装 PostgreSQL 16.6 的流程: |
|
| 12 | + |
|
| 13 | +#### 1. 下载安装包 |
|
| 14 | + |
|
| 15 | +- 访问 PostgreSQL 官方下载页面:[PostgreSQL 下载](https://www.postgresql.org/download/windows/) |
|
| 16 | +- 选择适合您系统的安装包(建议选择 `PostgreSQL Server` 和 `pgAdmin 4`)。 |
|
| 17 | + |
|
| 18 | +#### 2. 运行安装程序 |
|
| 19 | + |
|
| 20 | +- 双击下载的安装程序文件,启动安装向导。 |
|
| 21 | +- 在安装向导中,选择安装组件,包括 `PostgreSQL Server`、`pgAdmin 4` 和 `Command Line Tools` 等。 |
|
| 22 | + |
|
| 23 | +#### 3. 配置安装选项 |
|
| 24 | + |
|
| 25 | +- **选择安装目录**:默认安装目录为 `C:\Program Files\PostgreSQL\16`,您可以根据需要更改。 |
|
| 26 | +- **设置数据目录**:默认数据目录为 `C:\Program Files\PostgreSQL\16\data`,您可以根据需要更改。 |
|
| 27 | +- **设置超级用户密码**:输入并确认超级用户 `postgres` 的密码。 |
|
| 28 | +- **设置端口号**:默认端口号为 `5432`,您可以根据需要更改。 |
|
| 29 | +- **配置高级选项**:如需要,可以配置其他高级选项,如 `locale` 和 `encoding` 等。 |
|
| 30 | + |
|
| 31 | +#### 4. 完成安装 |
|
| 32 | + |
|
| 33 | +- 点击 `Install` 按钮开始安装。 |
|
| 34 | +- 安装完成后,您可以选择是否启动 `Stack Builder` 来安装其他工具和驱动程序。 |
|
| 35 | + |
|
| 36 | +#### 5. 验证安装 |
|
| 37 | + |
|
| 38 | +- 打开 `Dbeaver`,连接到本地服务器,数据库:`postgres` ,默认端口:`5432`, 输入超级用户 `postgres` 的密码,验证连接是否成功。 |
|
| 39 | +- 打开命令行工具,输入 `psql -U postgres -h localhost -p 5432`,验证是否可以成功登录到数据库。 |
|
| 40 | +- PostgreSQL 默认的数据库名字是`postgres`, 默认的模式`schema`是:`public`,pg的模式相当于MySQL的数据库,可以创建不同的模式。 |
|
| 41 | + |
|
| 42 | +#### 6. DBCP配置 |
|
| 43 | + |
|
| 44 | +``` |
|
| 45 | +########postgresql######## |
|
| 46 | +driverClassName=org.postgresql.Driver |
|
| 47 | +url=jdbc:postgresql://localhost:5432/postgres?currentSchema=public&encoding=UTF-8&timezone=UTC |
|
| 48 | +username=postgres |
|
| 49 | +password=passw0rd |
|
| 50 | + |
|
| 51 | +``` |
|
| 52 | + |
|
| 53 | +#### 参考资料 |
|
| 54 | + |
|
| 55 | +- [PostgreSQL 官方文档](https://www.postgresql.org/docs/) |
|
| 56 | + |
|
| 57 | +- [PostgreSQL 16 在 Windows 下安装](https://www.cnblogs.com/lwx11111/p/18375300) |
|
| 58 | + |
|
| 59 | +- [Windows 10 上安装 PostgreSQL 16](https://blog.csdn.net/fengbingchun/article/details/141750748) |
|
| 60 | + |
|
| 61 | +- [PostgreSQL 在 Windows 上的安装与配置](https://geek-docs.com/postgresql/postgresql-questions/218_tk_1704601434.html) |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + |
|
| 65 | +### 二、Docker安装PostgreSQL |
|
| 66 | + |
|
| 67 | +以下是在192.168.184.120 机器上,使用 Docker 安装 PostgreSQL 16.6 的流程: |
|
| 68 | + |
|
| 69 | +#### 1. 拉取镜像 |
|
| 70 | + |
|
| 71 | +从 Docker Hub 上拉取 PostgreSQL 16.6 的镜像: |
|
| 72 | + |
|
| 73 | +bash复制 |
|
| 74 | + |
|
| 75 | +```bash |
|
| 76 | +docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/postgres:16.6-alpine3.21 |
|
| 77 | +docker tag swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/postgres:16.6-alpine3.21 docker.io/postgres:16.6 |
|
| 78 | +``` |
|
| 79 | + |
|
| 80 | +#### 2. 创建并运行 PostgreSQL 容器 |
|
| 81 | + |
|
| 82 | +使用以下命令创建并运行一个 PostgreSQL 容器,并配置数据持久化: |
|
| 83 | + |
|
| 84 | +```bash |
|
| 85 | +docker run --name postgres16 -e POSTGRES_PASSWORD=passw0rd -p 5432:5432 -v /opt/postgresql/data:/var/lib/postgresql/data -d docker.io/postgres:16.6 |
|
| 86 | +``` |
|
| 87 | + |
|
| 88 | +- `--name postgres16`:指定容器名称为 `postgres16`。 |
|
| 89 | +- `-e POSTGRES_PASSWORD=mysecretpassword`:设置 PostgreSQL 的超级用户密码。 |
|
| 90 | +- `-p 5432:5432`:将容器的 5432 端口映射到主机的 5432 端口。 |
|
| 91 | +- `-v /opt/postgresql/data:/var/lib/postgresql/data`:将主机的目录(`/opt/postgresql/data`)挂载到容器的数据目录(`/var/lib/postgresql/data`),以实现数据持久化。 |
|
| 92 | + |
|
| 93 | +#### 3. 进入容器 |
|
| 94 | + |
|
| 95 | +如果需要进入容器内部,可以使用以下命令: |
|
| 96 | + |
|
| 97 | +bash复制 |
|
| 98 | + |
|
| 99 | +```bash |
|
| 100 | +docker exec -it postgres16 bash |
|
| 101 | +``` |
|
| 102 | + |
|
| 103 | +#### 5. 连接到数据库 |
|
| 104 | + |
|
| 105 | +在容器内部或宿主机上,可以使用 `psql` 命令连接到数据库: |
|
| 106 | + |
|
| 107 | +bash复制 |
|
| 108 | + |
|
| 109 | +```bash |
|
| 110 | +psql -U postgres -h localhost -p 5432 |
|
| 111 | +``` |
|
| 112 | + |
|
| 113 | +- `-U postgres`:指定用户名为 `postgres`。 |
|
| 114 | +- `-h localhost`:指定主机名为 `localhost`。 |
|
| 115 | +- `-p 5432`:指定端口号为 `5432`。 |
|
| 116 | + |
|
| 117 | +#### 6. DBCP配置 |
|
| 118 | + |
|
| 119 | +已经在192.168.184.120安装了PG,测试可以直接使用。PG默认的模式是:public,可以通过设置`currentSchema`切换模式。 |
|
| 120 | + |
|
| 121 | +``` |
|
| 122 | +########postgresql######## |
|
| 123 | +driverClassName=org.postgresql.Driver |
|
| 124 | +url=jdbc:postgresql://192.168.184.120:5432/postgres?currentSchema=public&encoding=UTF-8&timezone=UTC |
|
| 125 | +username=postgres |
|
| 126 | +password=passw0rd |
|
| 127 | + |
|
| 128 | +``` |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + |
|
| 132 | +### 参考资料 |
|
| 133 | + |
|
| 134 | +- [Docker 安装 PostgreSQL 16.6](https://developer.baidu.com/article/detail.html?id=2812489) |
|
| 135 | +- [Docker+PostgreSQL数据库](https://blog.csdn.net/shankezh/article/details/143241252) |
|
| 136 | +- [Docker 容器化部署 PostgreSQL](https://www.jianshu.com/p/c2f6759f3e75) |
|
| 137 | + |
|
| 138 | + |
|
| 7 | 139 | |
| 8 | 140 | |
| 9 | 141 | ## PostgreSQL 基础教程 |