19.1 PostgreSQL
安装 PostgreSQL
# pkg ins postgresql18-server# cd /usr/ports/databases/postgresql18-server/
# make install clean# pkg info -D postgresql18-server
postgresql18-server-18.r1:
软件包:postgresql18-server,版本 18.r1
On install:
安装时提示:
For procedural languages and postgresql functions, please note that
对于过程式语言与 PostgreSQL 函数,请注意:
you might have to update them when updating the server.
在升级数据库服务器时,这些扩展/函数也可能需要一并更新。
If you have many tables and many clients running, consider raising
如果表很多、并发客户端也很多,建议考虑提高系统允许打开文件的上限,
kern.maxfiles using sysctl(8), or reconfigure your kernel
可通过 sysctl(8) 调整 kern.maxfiles,或相应地重新配置内核,
appropriately.
以避免“文件描述符不足”等问题。
The port is set up to use autovacuum for new databases, but you might
该 Port 为新建数据库默认启用 autovacuum,但你仍可能需要
also want to vacuum and perhaps backup your database regularly. There
定期手动 vacuum,且定期备份数据库。系统提供了一个定时脚本,
is a periodic script, /usr/local/etc/periodic/daily/502.pgsql, that
位于 /usr/local/etc/periodic/daily/502.pgsql,
you may find useful. You can use it to backup and perform vacuum on all
你可以使用它对所有数据库进行备份并执行 vacuum,
databases nightly. Per default, it performs `vacuum analyze'. See the
默认是每晚执行一次;默认动作为 `vacuum analyze`。具体用法见脚本注释。
script for instructions. For autovacuum settings, please review
关于 autovacuum 的详细配置,请检查
~postgres/data/postgresql.conf.
~postgres/data/postgresql.conf(postgres 用户家目录下的数据目录配置文件)。
If you plan to access your PostgreSQL server using ODBC, please
如果计划通过 ODBC 访问 PostgreSQL 服务器,
consider running the SQL script /usr/local/share/postgresql/odbc.sql
请考虑运行 /usr/local/share/postgresql/odbc.sql 这个 SQL 脚本,
to get the functions required for ODBC compliance.
以创建 ODBC 兼容所需的函数。
Please note that if you use the rc script,
请注意:如果你使用 rc 脚本
/usr/local/etc/rc.d/postgresql, to initialize the database, unicode
/usr/local/etc/rc.d/postgresql 来初始化数据库,则默认会使用 Unicode
(UTF-8) will be used to store character data by default. Set
(UTF-8)来存储字符数据。你可以设置
postgresql_initdb_flags or use login.conf settings described below to
postgresql_initdb_flags,或者使用下文提到的 login.conf 设置,
alter this behaviour. See the start rc script for more info.
来改变这一默认行为。更多信息参见该启动脚本头部说明。
To set limits, environment stuff like locale and collation and other
若要设置资源限制、区域/排序等环境相关参数,
things, you can set up a class in /etc/login.conf before initializing
可以在初始化数据库前,先在 /etc/login.conf 中定义一个 login class,
the database. Add something similar to this to /etc/login.conf:
并将类似如下的条目加入 /etc/login.conf:
---
postgres:\
:lang=en_US.UTF-8:\
:setenv=LC_COLLATE=C:\
:tc=default:
---
上面这段是示例 login class:设置语言为 en_US.UTF-8,并将 LC_COLLATE 设为 C。
and run `cap_mkdb /etc/login.conf'.
随后运行 `cap_mkdb /etc/login.conf` 以生成数据库。
Then add 'postgresql_login_class="postgres"' to /etc/rc.conf, or
然后在 /etc/rc.conf 中加入 'postgresql_login_class="postgres"',
set it as the postgres user's login class in /etc/passwd.
或直接在 /etc/passwd 里将 postgres 用户的 login class 设为 postgres。
======================================================================
To use PostgreSQL, enable it in rc.conf using
要启用 PostgreSQL,请在 rc.conf 中打开服务:
sysrc postgresql_enable=yes
To initialize the database, run
初始化数据库:
service postgresql initdb
You can then start PostgreSQL by running:
随后启动 PostgreSQL:
service postgresql start
For postmaster settings, see ~postgres/data/postgresql.conf
关于 postmaster(服务进程)的设置,请查看 ~postgres/data/postgresql.conf。
NB. FreeBSD's PostgreSQL port logs to syslog by default
注意:FreeBSD 的 Port PostgreSQL 默认将日志写入 syslog,
See ~postgres/data/postgresql.conf for more info
详情见 ~postgres/data/postgresql.conf 中的日志相关配置。
NB. If you're not using a checksumming filesystem like ZFS, you might
注意:如果你没有使用带校验的文件系统(如 ZFS),
wish to enable data checksumming. It can be enabled during
可以考虑开启数据库的数据校验(checksumming)。该特性可在
the initdb phase, by adding the "--data-checksums" flag to
initdb 阶段通过添加 “--data-checksums” 参数启用,
the postgresql_initdb_flags rcvar. Otherwise you can enable it later by
也可以将该参数加入 rc 变量 postgresql_initdb_flags。若已完成初始化,
using pg_checksums. Check the initdb(1) manpage for more info
则可用 pg_checksums 事后开启。请参考 initdb(1) 手册获取更多信息,
and make sure you understand the performance implications.
并务必理解其对性能的影响(校验会带来额外开销)。服务管理
初始化数据库
登录使用
深入 PostgreSQL 服务管理
初始化建议
管理多个数据库实例(集簇)
配置实例
初始化实例目录
启动停止和重启
最后更新于