# cd /usr/ports/databases/postgresql16-server/
# make install clean
查看安装后配置
For procedural languages and postgresql functions, please note that
you might have to update them when updating the server.
# 如果你使用了过程语言或 PostgreSQL 函数,更新服务器时可能也需要同步更新它们。
If you have many tables and many clients running, consider raising
kern.maxfiles using sysctl(8), or reconfigure your kernel
appropriately.
# 如果你有大量表和客户端,建议使用 sysctl 提升 kern.maxfiles,或者重新配置内核参数。
The port is set up to use autovacuum for new databases, but you might
also want to vacuum and perhaps backup your database regularly. There
is a periodic script, /usr/local/etc/periodic/daily/502.pgsql, that
you may find useful. You can use it to backup and perform vacuum on all
databases nightly. Per default, it performs `vacuum analyze'. See the
script for instructions. For autovacuum settings, please review
~postgres/data/postgresql.conf.
# 端口默认对新数据库启用了 autovacuum,但你仍然应该定期手动 vacuum 和备份。
# 有一个每日脚本 /usr/local/etc/periodic/daily/502.pgsql 可以用来每晚备份和 vacuum 所有数据库。
# 默认执行 `vacuum analyze`,具体可查阅脚本。autovacuum 的设置请查看 postgresql.conf。
If you plan to access your PostgreSQL server using ODBC, please
consider running the SQL script /usr/local/share/postgresql/odbc.sql
to get the functions required for ODBC compliance.
# 若你打算通过 ODBC 访问 PostgreSQL,请运行 odbc.sql 脚本以安装所需的兼容函数。
Please note that if you use the rc script,
/usr/local/etc/rc.d/postgresql, to initialize the database, unicode
(UTF-8) will be used to store character data by default. Set
postgresql_initdb_flags or use login.conf settings described below to
alter this behaviour. See the start rc script for more info.
# 如果你用 rc 脚本初始化数据库,会默认使用 UTF-8 编码存储字符数据。
# 你可以通过设置 postgresql_initdb_flags 或 login.conf 来改变这个行为,详见 rc 脚本说明。
To set limits, environment stuff like locale and collation and other
things, you can set up a class in /etc/login.conf before initializing
the database. Add something similar to this to /etc/login.conf:
# 如果你需要设置资源限制、语言环境、排序规则等初始化参数,可以预先在 /etc/login.conf 里定义一个登录类。
Add something similar to this to /etc/login.conf:
---
postgres:\
:lang=en_US.UTF-8:\
:setenv=LC_COLLATE=C:\
:tc=default:
---
# 示例登录类设置,定义了语言和排序规则。
and run `cap_mkdb /etc/login.conf'.
Then add 'postgresql_login_class="postgres"' to /etc/rc.conf, or
set it as the postgres user's login class in /etc/passwd.
# 添加完登录类后运行 cap_mkdb 生效,并在 /etc/rc.conf 设置 postgresql_login_class。
# 或者直接在 /etc/passwd 中为 postgres 用户指定此登录类。
======================================================================
To use PostgreSQL, enable it in rc.conf using
sysrc postgresql_enable=yes
# 启用 PostgreSQL 服务。
To initialize the database, run
service postgresql initdb
# 初始化数据库。
You can then start PostgreSQL by running:
service postgresql start
# 启动 PostgreSQL 服务。
For postmaster settings, see ~postgres/data/postgresql.conf
# postmaster 设置在 postgresql.conf 中。
NB. FreeBSD's PostgreSQL port logs to syslog by default
See ~postgres/data/postgresql.conf for more info
# PostgreSQL 在 FreeBSD 上默认通过 syslog 记录日志,详见 postgresql.conf。
NB. If you're not using a checksumming filesystem like ZFS, you might
wish to enable data checksumming. It can be enabled during
the initdb phase, by adding the "--data-checksums" flag to
the postgresql_initdb_flags rcvar. Otherwise you can enable it later by
using pg_checksums. Check the initdb(1) manpage for more info
and make sure you understand the performance implications.
# 如果你不使用像 ZFS 这样的具备校验和的文件系统,建议开启 PostgreSQL 的数据校验功能。
# 你可以在 initdb 阶段用 --data-checksums 启用,或之后用 pg_checksums 工具开启。
# 请参考 initdb 手册并理解它可能带来的性能影响。
======================================================================
SECURITY ADVICE
# 安全建议
If upgrading from a version 16.x < 16.3
A security vulnerability was found in the system views pg_stats_ext
and pg_stats_ext_exprs, potentially allowing authenticated database
users to see data they shouldn't. If this is of concern in your
installation, run the SQL script /usr/local/share/postgresql/fix-CVE-2024-4317.sql
for each of your databases. For details, see
https://www.postgresql.org/support/security/CVE-2024-4317/
# 如果你从 16.3 以下版本升级,pg_stats_ext 和 pg_stats_ext_exprs 有安全漏洞,可能导致认证用户看到不该看的数据。
# 若你关心此问题,请对每个数据库执行 fix-CVE-2024-4317.sql。
# 详情见官方公告:https://www.postgresql.org/support/security/CVE-2024-4317/
加入启动项
# service postgresql enable
初始化数据库
# service postgresql initdb
示例输出:
root@ykla:~ # service postgresql initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with this locale configuration:
provider: libc
LC_COLLATE: C
LC_CTYPE: C.UTF-8
LC_MESSAGES: C.UTF-8
LC_MONETARY: C.UTF-8
LC_NUMERIC: C.UTF-8
LC_TIME: C.UTF-8
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /var/db/postgres/data16 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/usr/local/bin/pg_ctl -D /var/db/postgres/data16 -l logfile start
登录使用
Postgresql 默认是没有 root 用户的,需要使用其创建的用户 postgres 登录。
示例输出:
root@ykla:~ # psql
psql: FATAL: role "root" does not exist
正确用法:
#切换用户
root@ykla:~ # su - postgres
#启动服务
$ /usr/local/bin/pg_ctl -D /var/db/postgres/data16 -l logfile start
#创建新用户 ykla,并设置密码
$ createuser -sdrP ykla
Enter password for new role:
Enter it again:
$
#创建数据库
$ createdb new_db
#登录进数据库并将数据库权限赋予用户 ykla。
$ psql
psql (16.7)
Type "help" for help.
postgres=# ALTER USER ykla WITH ENCRYPTED PASSWORD 'password';
ALTER ROLE
postgres=#
postgres=# GRANT ALL PRIVILEGES ON DATABASE new_db TO ykla;
GRANT
#退出数据库
postgres=# q
$exit
root@ykla:~ #
深入 PostgreSQL 服务管理
初始化建议
初始化数据库建议是使用 service postgresql initdb,这是最简便的方式。但是里面还是有一些技巧可用。
# 增加一个 main 实例
# sysrc postgresql_profiles+=main
# 增加一个 dev 实例
# sysrc postgresql_profiles+=dev
# 当然可以直接一次设置多个实例
# sysrc postgresql_profiles="main dev"
# 对应 main 实例数据目录②
# sysrc postgresql_main_data=/var/db/postgres/main
# 对应 dev 实例数据目录③
# sysrc postgresql_dev_data=/var/db/postgres/dev
# 必须为每个实例设置数据目录
# (见上两条)
# 为 main 实例设置初始化参数①
# dev 实例默认初始化,作一对比
# sysrc postgresql_main_initdb_flags="-E utf-8 -A scram-sha-256 --lc-collate C -W"
# 为每个实例设置启用或关闭
# sysrc postgresql_main_enable=YES
# sysrc postgresql_dev_enable=YES
说明:
① 把实例名放入 rc 参数名中,就是对应实例的参数名。
② postgresql_data、postgresql_enable 等参数还是可以使用。这时这些参数作为实例对应参数的默认值使用,在找不到实例参数时使用。但当你显式配置了实例参数后,就不会使用默认参数了。
③ 在管理多个实例时,建议只使用实例名相关的参数,不去使用没有实例名的参数。好处是清晰明确,控制上也灵活,不同实例可以有不同设置。
初始化实例目录
# service postgresql initdb
这会初始化两个数据库实例。
也可以在命令最后指定实例名分别进行初始化,在新增实例时使用这种方法:
# service postgresql initdb main
# service postgresql initdb dev
注意:
两个不同的实例必须运行在不同的端口上,所以需要修改对应实例的端口号。
这里 main 实例使用默认的 5432 端口,不作修改;dev 实例使用 5433 端口,修改对应配置文件
修改 /var/db/postgres/dev/postgresql.conf。找到 port 行,修改如下:
port = 5433
启动停止和重启
# service postgresql start
# service postgresql status
# service postgresql restart
# service postgresql stop
以上针对所有实例进行。操作示例如下:
# service postgresql start # 启动服务
===> postgresql profile: main
start postgresql
===> postgresql profile: dev
start postgresql
# service postgresql status # 查看状态
===> postgresql profile: main
status postgresql
pg_ctl: server is running (PID: 2624)
/s/usr/local/bin/postgres "-D" "/var/db/postgres/main"
===> postgresql profile: dev
status postgresql
pg_ctl: server is running (PID: 2641)
/s/usr/local/bin/postgres "-D" "/var/db/postgres/dev"
# service postgresql restart # 重启服务
===> postgresql profile: main
restart postgresql
===> postgresql profile: dev
restart postgresql
# service postgresql status # 查看状态
===> postgresql profile: main
status postgresql
pg_ctl: server is running (PID: 2685)
/s/usr/local/bin/postgres "-D" "/var/db/postgres/main"
===> postgresql profile: dev
status postgresql
pg_ctl: server is running (PID: 2701)
/s/usr/local/bin/postgres "-D" "/var/db/postgres/dev"
# service postgresql stop # 停止服务
===> postgresql profile: main
stop postgresql
===> postgresql profile: dev
stop postgresql
# service postgresql status # 查看状态
===> postgresql profile: main
status postgresql
pg_ctl: no server running
===> postgresql profile: dev
status postgresql
pg_ctl: no server running
可以指定实例名单独操作:
# service postgresql start dev
# service postgresql restart dev
# service postgresql status dev
# service postgresql stop dev
输出示例如下:
# service postgresql start dev
start postgresql
# service postgresql status dev
status postgresql
pg_ctl: server is running (PID: 2807)
/s/usr/local/bin/postgres "-D" "/var/db/postgres/dev"
# service postgresql restart dev
restart postgresql
# service postgresql stop dev
stop postgresql
Using base prefix '/usr/local'
New python executable in /home/vagrant/pgadmin4/bin/python3.8
Also creating executable in /home/vagrant/pgadmin4/bin/python
Installing setuptools, pip, wheel…done.
done.
安装 sqlite3
#pkg install py38-sqlite3
激活创建的虚拟环境。
#source pgadmin4/bin/activate.csh
你会看到 shell 已经变为(pgadmin4)(以下操作均在该 shell 下进行)
实例如下:
(pgadmin4) root@ykla:~ #
安装 pgAdmin4
现在 pip 源一律要求使用 https,由于缺少 SSL 证书还需要安装。
(pgadmin4) root@ykla:~# pkg install ca_root_nss
然后对 pip 进行换源,此处使用清华源:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
其中有依赖 openjpeg,先进行安装
(pgadmin4) root@ykla:~# pkg install openjpeg
如果报错:
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate is not yet valid (_ssl.c:1136)'))': /simple/pgadmin4/
是由于时间不正确引发的,先同步时间:
ntpdate ntp.api.bz
(pgadmin4) root@ykla:~# ntpdate ntp.api.bz
17 Dec 16:35:36 ntpdate[1453]: step time server 114.118.7.161 offset +401965.911037 sec
NOTE: Configuring authentication for SERVER mode.
Enter the email address and password to use for the initial pgAdmin user account:
Email address: your_email # 输入你的邮件地址
Password: your_new_password # 输入你的登录密码
Retype password: # 再次输入密码
Starting pgAdmin 4. Please navigate to http://0.0.0.0:5050 in your browser.
现在我们已经安装并运行了 pgAdmin4,并可以通过 http://ip:5050 访问 Web 控制面板: