Skip to main content

将Backstage从 SQLite 切换到 PostgreSQL

默认值@backstage/create-appSQLite 是一种内存数据库,无需环境设置,非常适合初始实验。

一旦准备好在生产环境中部署 Backstage,或拥有一个更持久的开发设置,就可以将 Backstage 数据库切换到 PostgreSQL。

Backstage使用克耐克斯库,使数据库后端之间的切换变得相当容易。

安装 PostgreSQL

首先,将 PostgreSQL 添加到backend包装

# From your Backstage root directory
yarn add --cwd packages/backend pg

添加 PostgreSQL 配置

接下来,修改app-config.yaml在根文件夹中添加 PostgreSQL 后端配置:

app-config.yaml
backend:
database:
client: better-sqlite3
connection: ':memory:'
# config options: https://node-postgres.com/apis/client
client: pg
connection:
host: ${POSTGRES_HOST}
port: ${POSTGRES_PORT}
user: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
# https://node-postgres.com/features/ssl
# you can set the sslmode configuration option via the `PGSSLMODE` environment variable
# see https://www.postgresql.org/docs/current/libpq-ssl.html Table 33.1. SSL Mode Descriptions (e.g. require)
# ssl:
# ca: # if you have a CA file and want to verify it you can uncomment this section
# $file: <file-path>/ca/server.crt

如果您有app-config.local.yaml您可以设置POSTGRES_环境变量,或者在启动Backstage之前删除${...}值,只需直接设置实际值进行开发即可。

现在,Backstage应用程序已准备好使用 PostgreSQL 后备数据库启动。

覆盖默认的 PostgreSQL 数据库池配置

如果要覆盖默认连接池设置,请使用下面的配置:

app-config.local.yaml
backend:
database:
client: better-sqlite3
connection: ':memory:'
# config options: https://node-postgres.com/apis/client
client: pg
connection:
host: ${POSTGRES_HOST}
port: ${POSTGRES_PORT}
user: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
# https://node-postgres.com/features/ssl
# you can set the sslmode configuration option via the `PGSSLMODE` environment variable
# see https://www.postgresql.org/docs/current/libpq-ssl.html Table 33.1. SSL Mode Descriptions (e.g. require)
# ssl:
# ca: # if you have a CA file and want to verify it you can uncomment this section
# $file: <file-path>/ca/server.crt
# Refer to Tarn docs for default values on PostgreSQL pool configuration - https://github.com/Vincit/tarn.js
knexConfig:
pool:
min: 3
max: 12
acquireTimeoutMillis: 60000
idleTimeoutMillis: 60000

使用单一数据库

默认情况下,每个插件都将获得自己的逻辑数据库,以确保您安装的所有插件的表名不会发生冲突,并将它们的关注点分开,以方便以后的其他使用情况。 如果您受到限制,只能使用单个数据库,您可以使用一个特殊选项pluginDivisionModeclient: pg在配置中创建单独的PostgreSQL 模式而不是创建单独的数据库。

您可以通过以下配置启用此功能:

backend:
database:
client: pg
pluginDivisionMode: schema # defaults to database, but changing this to schema means plugins will be given their own schema (in the specified/default database)