Files
container/部署PostGIS.md

165 lines
4.0 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;
2026-03-02 13:51:38 +08:00
CREATE TEXT SEARCH CONFIGURATION chinese_mix (PARSER = zhparser);
-- DROP TEXT SEARCH CONFIGURATION IF EXISTS chinese_mix;
2026-03-02 13:45:31 +08:00
2026-03-02 13:51:38 +08:00
ALTER TEXT SEARCH CONFIGURATION chinese_mix
2026-03-02 13:55:40 +08:00
ADD MAPPING FOR n,v,a,i,e,l,d,j,m,q,r,t,u,w,x,z WITH simple;
2026-03-02 13:51:38 +08:00
SELECT to_tsvector('chinese_mix', 'Hello world, 你好世界PostgreSQL 很棒');
2026-03-02 13:45:31 +08:00
```
## 测试
```sql
-- 创建测试表
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
```