# 快速上手

> 准备数据库，以 PostgreSQL 后端启动 Silo，创建桶和对象。

---

LLMS 索引： [llms.txt](/zh/llms.txt)

---

> [!NOTE]
> Silo 的 PostgreSQL 后端（`pgsilo`）正处于 MVP 开发中，安装包尚未公开发布。下文的包名与命令来自 schema 14 本地构建，目前没有公开下载地址。

本指南准备 PostgreSQL 数据库，以 PostgreSQL 模式启动可写的 Silo 网关，创建桶与受限应用用户，并通过 S3 和 SQL 读写对象。完整的主机、证书与 macOS 配置见[安装](/zh/docs/s3/operations/install/)。

## 准备 PostgreSQL {#prepare-postgresql}

使用 PostgreSQL 16 或更高版本。网关应直接连接可写主库，不要经过事务池模式的 PgBouncer。

创建普通数据库所有者与 `silo` 数据库：

```sql
CREATE ROLE silo_owner LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION;
\password silo_owner
CREATE DATABASE silo OWNER silo_owner;
```

首个可写网关以 `silo_owner` 身份连接数据库，在单个事务里执行 0001–0014 迁移，创建 `silo`、`silo_api` 与 `silo_bucket` 三个 schema。

## 启动网关 {#start-gateway}

设置 `SILO_PG_DSN` 或只传入一个 `postgres://` URI（不带磁盘路径参数）时，Silo 会进入 PostgreSQL 模式。

在 Debian 或 Ubuntu 上使用本地安装包时，先校验哈希再安装：

```bash
sha256sum -c SHA256SUMS
sudo dpkg -i ./silo_20261007153132.0.0-1PGSTY_arm64.deb
```

在 `/etc/default/silo` 中写入数据库 DSN、根凭据与连接池上限（启动服务前请替换全部占位符）：

```ini
# systemd EnvironmentFile；启动服务前请替换占位符。
SILO_PG_DSN="postgresql://silo_owner:URL_ENCODED_PASSWORD@pg.example.net:5432/silo?sslmode=verify-full&sslrootcert=/etc/silo/postgres-ca.crt"
MINIO_ROOT_USER="REPLACE_WITH_ROOT_ACCESS_KEY"
MINIO_ROOT_PASSWORD="REPLACE_WITH_ROOT_SECRET"
MINIO_REGION="us-east-1"
SILO_PG_READ_ONLY="off"
SILO_PG_MAX_CONNS="32"
SILO_PG_CONTROL_CONNS="4"
SILO_PG_COORD_CONNS="2"
SILO_PG_SEGMENT_SIZE="8MiB"
SILO_PG_MAX_OBJECT_SIZE="256MiB"
SILO_PG_ACQUIRE_TIMEOUT="5s"
SILO_PG_REQUEST_TIMEOUT="15m"
```

安装 systemd 覆盖配置 `/etc/systemd/system/silo.service.d/postgresql.conf`，使服务不带磁盘参数启动：

```ini
[Service]
EnvironmentFile=
EnvironmentFile=/etc/default/silo
ExecStart=
ExecStart=/usr/bin/silo server --address :9000 --console-address :9001 --certs-dir /etc/silo/certs
```

启用并启动服务，然后检查读写就绪状态：

```bash
sudo systemctl daemon-reload && sudo systemctl enable --now silo
curl --fail --cacert /path/to/s3-ca.crt https://s3.example.net:9000/minio/health/cluster/read
curl --fail --cacert /path/to/s3-ca.crt https://s3.example.net:9000/minio/health/cluster
```

健康的可写网关在两个端点均返回 HTTP 200。全部 `SILO_PG_*` 变量说明见[配置](/zh/docs/s3/reference/configuration/)。

## 创建桶与应用用户 {#create-bucket}

安装 [`mcli`](https://github.com/pgsty/mc/releases)，配置信任网关 CA，并用根凭据设置管理别名：

```bash
export SILO_ENDPOINT='https://s3.example.net:9000'
export SILO_CLIENT_CONFIG="$HOME/.config/silo-tutorial/mcli"
umask 077
mkdir -p "$SILO_CLIENT_CONFIG/certs/CAs"
cp /path/to/s3-ca.crt "$SILO_CLIENT_CONFIG/certs/CAs/silo-ca.crt"
mcli_pg() { command mcli --config-dir "$SILO_CLIENT_CONFIG" "$@"; }
mcli_pg alias set pg-admin "$SILO_ENDPOINT" --api S3v4 --path on
```

创建 `attachments` 桶与应用用户 `app-s3`：

```bash
mcli_pg mb pg-admin/attachments
mcli_pg admin user add pg-admin
```

将仅限该桶的访问策略保存为 `~/.local/share/silo-tutorial/attachments-rw.json`：

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:GetBucketLocation", "s3:ListBucket"],
      "Resource": ["arn:aws:s3:::attachments"]
    },
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
      "Resource": ["arn:aws:s3:::attachments/*"]
    }
  ]
}
```

创建并绑定策略，再用 `app-s3` 凭据配置应用别名 `pg-app`：

```bash
mcli_pg admin policy create pg-admin attachments-rw "$HOME/.local/share/silo-tutorial/attachments-rw.json"
mcli_pg admin policy attach pg-admin attachments-rw --user app-s3
mcli_pg alias set pg-app "$SILO_ENDPOINT" --api S3v4 --path on
```

## 通过 S3 上传、读取与删除 {#use-s3}

上传文件，列举并查看对象元数据，下载后比对内容，最后删除对象：

```bash
export SILO_TUTORIAL_DIR="$HOME/.local/share/silo-tutorial/files"
mkdir -p "$SILO_TUTORIAL_DIR"
printf 'Hello Silo PostgreSQL\n' > "$SILO_TUTORIAL_DIR/hello.txt"
mcli_pg cp "$SILO_TUTORIAL_DIR/hello.txt" pg-app/attachments/hello.txt
mcli_pg ls pg-app/attachments
mcli_pg stat pg-app/attachments/hello.txt
mcli_pg cp pg-app/attachments/hello.txt "$SILO_TUTORIAL_DIR/hello.download.txt"
cmp "$SILO_TUTORIAL_DIR/hello.txt" "$SILO_TUTORIAL_DIR/hello.download.txt"
mcli_pg rm pg-app/attachments/hello.txt
```

## 从 SQL 写入对象 {#use-sql}

> [!NOTE]
> SQL 事务接口（`silo_api`）在 MVP 范围内属于**预览**功能。

在拥有 `silo_api` 与桶 ACL 关系 `silo_bucket.attachments` 权限的 `psql` 会话中，可以在 Read Committed 事务里直接写入对象：

```sql
BEGIN;
SELECT * FROM silo_api.put_object(
  'attachments',
  'invoice/2026-001.txt',
  convert_to('paid', 'UTF8'),
  'text/plain'
);
COMMIT;
```

事务提交后，立即可以通过 S3 读取 `attachments/invoice/2026-001.txt`。权限授予与函数签名见[用 SQL 操作对象](/zh/docs/s3/tutorial/sql/)和 [SQL 接口参考](/zh/docs/s3/reference/sql-api/)。

## 打开控制台 {#console}

在信任网关 CA 的浏览器中打开 `https://console.example.net:9001`，使用根凭据登录。在 **Buckets → attachments → Objects** 中查看或上传文件，在用户与策略页面管理 IAM。

控制台显示的逻辑对象用量不等于 PostgreSQL 表与 WAL 的物理磁盘大小。PostgreSQL 模式下物理容量显示为 `Unknown` 属于正常现象，不代表剩余空间为零。

---

反链：

- [安装](/zh/docs/s3/operations/install/)
- [教程](/zh/docs/s3/tutorial/)
- [使用 S3 SDK](/zh/docs/s3/tutorial/sdk/)
- [用 SQL 操作对象](/zh/docs/s3/tutorial/sql/)
- [版本控制](/zh/docs/s3/tutorial/versioning/)
