安装
docker-compose安装
cat <<EOF > docker-compose.yaml
version: '3.8'
services:
db:
image: harbor.qihoo.net/finloan-infra-library/postgres:15-alpine
restart: always
environment:
POSTGRES_USER: postgres
# The password for the default postgres user.
POSTGRES_PASSWORD: difyai123456
# The name of the default postgres database.
POSTGRES_DB: dify
# postgres data directory
PGDATA: /var/lib/postgresql/data/pgdata
shm_size: '16gb' # shared memory
command: postgres -c 'max_connections=10000'
volumes:
- /data/postgre:/var/lib/postgresql/data
# uncomment to expose db(postgresql) port to host
ports:
- "5432:5432"
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 1s
timeout: 3s
retries: 30
EOF
docker compose up -d
运维
修改最大连接数
pg默认最大连接数是100,如果你的pod已经启动过了,需要去修改配置文件才能生效;
psql
# 查看配置文件所在路径: /var/lib/postgresql/data/pgdata/postgresql.conf
show config_file;
exit
# 修改配置文件中的最大连接数
sed -i 's/^max_connections[ ]*=.*.*/max_connections = 10000/' /var/lib/postgresql/data/pgdata/postgresql.conf
# 重启容器
docker compose restart
常用命令
# 连接当前库
psql
# 连接指定库
psql -h localhost -p 5432 -U postgres -d dify
# 列出所有数据库
\l
# 当前链接的库
select current_database();
# 连接到其他数据库
\c otherdb
# 查看数据库的模式
\dn
# 列出所有的表
\dt
# 查看特定的表结果
\d tablename
\d+ tablename
# 显示当前连接信息
\conninfo
# 显示命令历史
\s
# 重复执行上一条命令
\g
# 保存查询结果到文件
\o filename
select * from mytable;
\o
# 执行文件中的sql
\i filename.sql