Files
container/部署PostGIS.md

176 lines
4.5 KiB
Markdown
Raw Normal View History

2026-02-27 11:09:00 +08:00
## 目录结构
- pgsql-gis/
- pgsql-admin/
- docker-compose.yaml
```shell
mkdir pgsql-gis pgsql-admin
sudo chown 5050 ./pgsql-admin
```
## docker-compose.yaml
```yaml
version: '3.8'
services:
# PGSQL
pgsql-gis-16:
2026-03-02 13:34:59 +08:00
# 不支持全文检索
2026-02-27 11:09:00 +08:00
image: postgis/postgis:16-master
2026-03-02 13:34:59 +08:00
# 支持全文检索的自编译镜像
#image: pgsql-gis-fts:latest
2026-02-27 11:09:00 +08:00
container_name: pgsql-gis-16
ports:
- "35430:5432" # 替换
volumes:
- ./pgsql-gis:/var/lib/postgresql/data
environment:
POSTGRES_USER: # 替换
POSTGRES_PASSWORD: # 替换
POSTGRES_DB: # 替换
POSTGRES_INITDB_ARGS: --encoding=UTF8
restart: on-failure:3
# PG AdminHTTP
pgsql-admin:
image: dpage/pgadmin4:latest # 替换
container_name: pgsql-admin
ports:
- "18906:80" # 替换
volumes:
- ./pgsql-admin:/var/lib/pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: demo@email.com
PGADMIN_DEFAULT_PASSWORD: # 替换
PGADMIN_CONFIG_CHECK_EMAIL_DELIVERABILITY: 'False'
restart: on-failure:3
# PG AdminHTTPS
# pgsql-admin:
# image: dpage/pgadmin4:latest
# container_name: pgsql-admin
# ports:
# - "18907:443"
# volumes:
# - ./pgsql-admin:/var/lib/pgadmin
# - ./certs:/certs
# environment:
# PGADMIN_DEFAULT_EMAIL: demo@email.com
# PGADMIN_DEFAULT_PASSWORD: # 替换
# PGADMIN_CONFIG_CHECK_EMAIL_DELIVERABILITY: 'False'
# PGADMIN_ENABLE_TLS: 'True'
# PGADMIN_SERVER_CERT_FILE: /certs/server.cert
# PGADMIN_SERVER_KEY_FILE: /certs/server.key
# restart: on-failure:3
2026-03-02 13:32:33 +08:00
```
## 添加全文索引支持
需要从 git 克隆 zhparser 添加中文分词支持,执行 docker build 命令的目录结构长这样:
- Dockerfile
- zhparser/
```bash
git clone https://github.com/amutu/zhparser.git
```
2026-03-02 13:33:10 +08:00
## Dockerfile
2026-03-02 13:32:33 +08:00
```bash
FROM postgis/postgis:16-master
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
build-essential \
git \
libcurl4-openssl-dev \
libxml2-dev \
wget \
postgresql-server-dev-16 \
&& rm -rf /var/lib/apt/lists/*
RUN pg_config --version
WORKDIR /tmp
RUN wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2 \
&& tar xjf scws-1.2.3.tar.bz2 \
&& cd scws-1.2.3 \
&& ./configure \
&& make && make install \
&& cd ..
COPY zhparser zhparser
RUN cd zhparser \
&& export PG_CONFIG=$(which pg_config) \
&& echo "Using pg_config: $PG_CONFIG" \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install \
&& cd ..
RUN rm -rf /tmp/*
WORKDIR /
```
2026-03-02 13:33:31 +08:00
## 编译
2026-03-02 13:33:10 +08:00
2026-03-02 13:32:33 +08:00
```bash
docker build -t pgsql-gis-fts .
2026-03-02 13:45:31 +08:00
```
## 启用中文分词扩展
```sql
-- 创建扩展
CREATE EXTENSION IF NOT EXISTS zhparser;
```
```sql
-- 创建一个新的文本搜索配置 (命名为 chinese_mix)
-- 基于内置的 simple 配置(它能很好地处理英文和标点)
CREATE TEXT SEARCH CONFIGURATION chinese_mix (COPY = simple);
```
```sql
-- 将中文分词器添加到该配置中
-- 这条命令的意思是:对于识别为中文名词、动词等的 token使用 zhparser 进行分词
-- 其他类型(如英文单词、数字)继续使用原有的 simple 处理
ALTER TEXT SEARCH CONFIGURATION chinese_mix ADD MAPPING FOR n,v,a,i,e,l WITH zhparser;
```
## 测试
```sql
-- 测试分词效果
SELECT to_tsvector('chinese_mix', 'PostgreSQL 是一个强大的开源数据库,支持中文全文检索!');
-- 预期输出应该包含 'postgresql', '强大', '开源', '数据库', '支持', '中文', '全文', '检索' 等词
-- 创建测试表
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT
);
-- 插入测试数据 (中英混合)
INSERT INTO articles (title, content) VALUES
('Introduction to AI', 'Artificial Intelligence is changing the world.'),
('人工智能简介', '人工智能正在改变世界PostgreSQL 是很好的存储工具。'),
('PG 16 New Features', 'PostgreSQL 16 brings better performance and json features.');
-- 创建 GIN 索引 (加速检索的关键)
CREATE INDEX idx_articles_content ON articles USING GIN (to_tsvector('chinese_mix', content));
-- 执行搜索查询
-- 搜索 "人工智能"
SELECT title FROM articles WHERE to_tsvector('chinese_mix', content) @@ to_tsquery('chinese_mix', '人工智能');
-- 搜索 "PostgreSQL" (英文)
SELECT title FROM articles WHERE to_tsvector('chinese_mix', content) @@ to_tsquery('chinese_mix', 'PostgreSQL');
-- 搜索混合条件
SELECT title FROM articles WHERE to_tsvector('chinese_mix', content) @@ to_tsquery('chinese_mix', '数据库 & 改变');
2026-02-27 11:09:00 +08:00
```