PostgreSQL installed in FreeBSD

本文涉及的产品
运维安全中心(堡垒机),免费版 6个月
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
运维安全中心(堡垒机),企业双擎版|50资产|一周时长
简介:
上一篇BLOG介绍了freebsd的安装, 
本文将介绍一下freebsd下编译安装postgresql.
首先要下载postgresql代码, 解压, 安装.
root@digoal:~ # fetch http://ftp.postgresql.org/pub/source/v9.3.4/postgresql-9.3.4.tar.bz2
root@digoal:~ # tar -jxvf postgresql-9.3.4.tar.bz2
root@digoal:~ # cd postgresql-9.3.4
root@digoal:~/postgresql-9.3.4 # ./configure --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety

使用这个configure选项的话, 会缺少很多包, 所以要根据configure的返回安装对应的包.
首先会缺少bison, 但是PG不会因为缺少bison不允许安装, 所以只是WARNING, 缺少一些功能, 需要的话可以安装bison
root@my-domain:~ # pkg search bison
bison-2.7.1,1
bisoncpp-4.09.01
py27-bison-0.1.8_3
ruby19-rbison-0.0.7_3
root@my-domain:~ # pkg install bison

接下来, 
checking for perl... no
configure: WARNING:
*** Without Perl you will not be able to build PostgreSQL from Git.
*** You can obtain Perl from any CPAN mirror site.
*** (If you are using the official distribution of PostgreSQL then you do not
*** need to worry about this, because the Perl output is pre-generated.)
configure: error: Perl not found

说明缺少perl的包, 可以使用pkg search查找一下包名. 然后安装
root@digoal:~ # pkg search perl

如果search没有结果, 可能是pkg的repository没有配置好, 可以update一下.
root@digoal:~ # pkg update
Updating repository catalogue
digests.txz                                                                             100% 1085KB  67.8KB/s 185.3KB/s   00:16    
packagesite.txz                                                                         100% 5007KB 250.3KB/s 376.6KB/s   00:20    
Incremental update completed, 23207 packages processed:
0 packages updated, 0 removed and 23207 added.


再次搜索就有了.
root@digoal:~ # pkg search perl
Hyperlatex-2.9.a_4
amanda-perl-wrapper-1.01
ap22-mod_perl2-2.0.8,3
claws-mail-perl-3.10.0
clipperlib-6.0.0
dnsmax-perl-1.0.1
libmp3-archive-perl-0.9
libproxy-perl-0.4.6
libtext-charwidth-perl-0.04
libtext-wrapi18n-perl-0.06
mldonkey-perlreactor-0.2
nzbperl-0.6.8_1
p5-DBIx-Log4perl-0.26
p5-Dancer-Logger-Log4perl-0.8.1
p5-Log-Any-Adapter-Log4perl-0.06
p5-Log-Log4perl-1.42
p5-Log-Log4perl-Appender-RabbitMQ-0.102220
p5-MooseX-Log-Log4perl-0.46
p5-Script-isAperlScript-1.0.0
p5-Test-Log4perl-0.1001
p5-bioperl-1.6.923
p5-eperl-2.2.14
p5-jmx4perl-1.07_1
p5-perl-ldap-0.5800
p5-perlbrew-0.59
p5-perlkde-4.12.5
p5-perlqt-4.12.5
p5-rpm-build-perl-0.82
p5-uni-perl-0.91
pecl-perl-1.0.1
perl2html-0.9.2_1
perl5-5.16.3_10
perl5.14-5.14.4_7
perl5.18-5.18.2_3
perl5.20-5.20.0_2
perlconsole-0.4_1
postfix-policyd-spf-perl-2.010
postgresql84-plperl-8.4.21_1
postgresql9-plperl-9.4.b1
postgresql90-plperl-9.0.17
postgresql91-plperl-9.1.13_1
postgresql92-plperl-9.2.8_1
postgresql93-plperl-9.3.4
sooperlooper-1.7.0_1
superlu-3.0.20060201_6
superlu_mt-2.0.20080115_5
urxvt-perls-2.0_1

不建议安装最新的

root@digoal:~ # pkg install perl5.20-5.20.0_2
Updating repository catalogue
The following 1 packages will be installed:
        Installing perl5.20: 5.20.0_2
The installation will require 48 MB more space
13 MB to be downloaded
Proceed with installing packages [y/N]: y

重新执行configure, 看看还缺少什么包, 继续安装.
root@digoal:~/postgresql-9.3.4 # ./configure --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety

checking for python... no
configure: error: Python not found


这次少了python.
root@digoal:~/postgresql-9.3.4 # pkg search python
python-2.7_1,2
root@digoal:~/postgresql-9.3.4 # pkg install python-2.7_1,2
Updating repository catalogue
The following 4 packages will be installed:
        Installing gettext: 0.18.3.1_1
        Installing python27: 2.7.6_4
        Installing python2: 2_2
        Installing python: 2.7_1,2
The installation will require 78 MB more space
12 MB to be downloaded
Proceed with installing packages [y/N]: y
gettext-0.18.3.1_1.txz                                                                  100% 2078KB  79.9KB/s 268.0KB/s   00:26    
python27-2.7.6_4.txz                                                                    100% 9997KB 217.3KB/s 177.0KB/s   00:46    
python2-2_2.txz                                                                         100% 2336     2.3KB/s   2.3KB/s   00:00    
python-2.7_1,2.txz                                                                      100% 2288     2.2KB/s   2.2KB/s   00:00    
Checking integrity... done
[1/4] Installing gettext-0.18.3.1_1... done
[2/4] Installing python27-2.7.6_4... done
[3/4] Installing python2-2_2... done
[4/4] Installing python-2.7_1,2... done
====
Note that some of the standard modules are provided as separate
ports since they require extra dependencies:

bsddb           databases/py-bsddb
gdbm            databases/py-gdbm
sqlite3         databases/py-sqlite3
tkinter         x11-toolkits/py-tkinter

Install them as needed.
====


重新执行configure, 看看还缺少什么包, 继续安装.
root@digoal:~/postgresql-9.3.4 # ./configure --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety

checking for xmlSaveToBuffer in -lxml2... no
configure: error: library 'xml2' (version >= 2.6.23) is required for XML support


这次缺少的 xml2
root@digoal:~/postgresql-9.3.4 # pkg search xml2
xml2-0.5_1
root@digoal:~/postgresql-9.3.4 # pkg install xml2-0.5_1
Updating repository catalogue
The following 2 packages will be installed:
        Installing libxml2: 2.9.1_1
        Installing xml2: 0.5_1
The installation will require 4 MB more space
772 KB to be downloaded
Proceed with installing packages [y/N]: y
libxml2-2.9.1_1.txz                                                                     100%  753KB 150.6KB/s  92.7KB/s   00:05    
xml2-0.5_1.txz                                                                          100%   19KB  19.2KB/s  19.2KB/s   00:01    
Checking integrity... done
[1/2] Installing libxml2-2.9.1_1... done
[2/2] Installing xml2-0.5_1... done



接下来缺少xslt
root@digoal:~/postgresql-9.3.4 # ./configure --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
checking for xsltCleanupGlobals in -lxslt... no
configure: error: library 'xslt' is required for XSLT support

root@digoal:~/postgresql-9.3.4 # pkg search xslt
libxslt-1.1.28_3
root@digoal:~/postgresql-9.3.4 # pkg install libxslt-1.1.28_3
Updating repository catalogue
The following 3 packages will be installed:

        Installing libgpg-error: 1.13_1
        Installing libgcrypt: 1.5.3_2
        Installing libxslt: 1.1.28_3

The installation will require 2 MB more space

621 KB to be downloaded

Proceed with installing packages [y/N]: y
libgpg-error-1.13_1.txz                                                                 100%   73KB  73.0KB/s  23.0KB/s   00:01    
libgcrypt-1.5.3_2.txz                                                                   100%  335KB 111.6KB/s 224.8KB/s   00:03    
libxslt-1.1.28_3.txz                                                                    100%  213KB  70.9KB/s  52.8KB/s   00:03    
Checking integrity... done
[1/3] Installing libgpg-error-1.13_1... done
[2/3] Installing libgcrypt-1.5.3_2... done
[3/3] Installing libxslt-1.1.28_3... done


注意libxslt安装完后, 报缺少libxslt/xslt.h头文件
root@digoal:~/postgresql-9.3.4 # ./configure --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
checking for libxslt/xslt.h... no
configure: error: header file <libxslt/xslt.h> is required for XSLT support

但是这个头文件实际上已经存在了
root@digoal:~/postgresql-9.3.4 # find / -name xslt.h
/usr/local/include/libxslt/xslt.h


使用CPPFLAGS设置include文件夹
./configure --help
Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    C/C++/Objective C preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CPP         C preprocessor
  LDFLAGS_EX  extra linker flags for linking executables only
  LDFLAGS_SL  extra linker flags for linking shared libraries only
  DOCBOOKSTYLE
              location of DocBook stylesheets


重新执行configure, 并设置CPPFLAGS. 检查通过.
root@digoal:~/postgresql-9.3.4 # ./configure CPPFLAGS='-I/usr/local/include' --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
checking libxslt/xslt.h usability... yes
checking libxslt/xslt.h presence... yes

报缺少ldap.h
checking for ldap.h... no
configure: error: header file <ldap.h> is required for LDAP

安装openldap
root@digoal:~/postgresql-9.3.4 # pkg search ldap  
openldap-server-2.4.39_1
root@digoal:~/postgresql-9.3.4 # pkg install openldap-server-2.4.39_1
Updating repository catalogue
The following 4 packages will be installed:
        Installing openldap-client: 2.4.39
        Installing libltdl: 2.4.2_3
        Installing db46: 4.6.21.4
        Installing openldap-server: 2.4.39_1
The installation will require 39 MB more space
7 MB to be downloaded
Proceed with installing packages [y/N]: y
openldap-client-2.4.39.txz                                                              100%  995KB  58.5KB/s 184.6KB/s   00:17    
libltdl-2.4.2_3.txz                                                                     100%   37KB  36.8KB/s  36.8KB/s   00:00    
db46-4.6.21.4.txz                                                                       100% 5218KB 173.9KB/s 437.9KB/s   00:30    
openldap-server-2.4.39_1.txz                                                            100%  884KB 126.4KB/s 224.4KB/s   00:07    
Checking integrity... done
[1/4] Installing openldap-client-2.4.39... done
[2/4] Installing libltdl-2.4.2_3... done
[3/4] Installing db46-4.6.21.4... done
[4/4] Installing openldap-server-2.4.39_1...===> Creating users and/or groups.
Creating group 'ldap' with gid '389'.
Creating user 'ldap' with uid '389'.
 done
************************************************************

The OpenLDAP client package has been successfully installed.

Edit
  /usr/local/etc/openldap/ldap.conf
to change the system-wide client defaults.

Try `man ldap.conf' and visit the OpenLDAP FAQ-O-Matic at
  http://www.OpenLDAP.org/faq/index.cgi?file=3
for more information.

************************************************************


************************************************************

The OpenLDAP server package has been successfully installed.

In order to run the LDAP server, you need to edit
  /usr/local/etc/openldap/slapd.conf
to suit your needs and add the following lines to /etc/rc.conf:
  slapd_enable="YES"
  slapd_flags='-h "ldapi://%252fvar%252frun%252fopenldap%252fldapi/ ldap://0.0.0.0/"'
  slapd_sockets="/var/run/openldap/ldapi"

Then start the server with
  /usr/local/etc/rc.d/slapd start
or reboot.

Try `man slapd' and the online manual at
  http://www.OpenLDAP.org/doc/
for more information.
  
slapd runs under a non-privileged user id (by default `ldap'),
see /usr/local/etc/rc.d/slapd for more information.

************************************************************


接下来是未找到tcl和tclsh, 注意版本.
checking for tclsh... no
checking for tcl... no
checking for tclsh8.5... no
checking for tclsh85... no
checking for tclsh8.4... no
checking for tclsh84... no
checking for tclsh8.3... no
checking for tclsh83... no
configure: error: Tcl shell not found

root@digoal:~/postgresql-9.3.4 # pkg search tcl
tcl84-8.4.20_2,1
tcl85-8.5.15_2
tcl86-8.6.1
root@digoal:~/postgresql-9.3.4 # pkg install tcl85-8.5.15_2
Updating repository catalogue
The following 1 packages will be installed:
        Installing tcl85: 8.5.15_2
The installation will require 6 MB more space
920 KB to be downloaded
Proceed with installing packages [y/N]: y


好了, 终于完全通过检测了
root@digoal:~/postgresql-9.3.4 # ./configure CPPFLAGS='-I/usr/local/include' --prefix=/opt/pgsql9.3.4 --with-pgport=1921 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
.......
configure: using compiler=FreeBSD clang version 3.3 (tags/RELEASE_33/final 183502) 20130610
configure: using CFLAGS=-O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv
configure: using CPPFLAGS=-I/usr/local/include -I/usr/local/include/libxml2 -I/usr/include 
configure: using LDFLAGS= -L/usr/local/lib -L/usr/lib  -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/dynloader/freebsd.c to src/backend/port/dynloader.c
config.status: linking src/backend/port/sysv_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/backend/port/unix_latch.c to src/backend/port/pg_latch.c
config.status: linking src/backend/port/dynloader/freebsd.h to src/include/dynloader.h
config.status: linking src/include/port/freebsd.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.freebsd to src/Makefile.port


使用gmake, 如果缺少的话, 使用pkg安装.
root@digoal:~/postgresql-9.3.4 # gmake world                                                                                        gmake: Command not found.
root@digoal:~/postgresql-9.3.4 # pkg search gmake
gmake-3.82_1
root@digoal:~/postgresql-9.3.4 # pkg install gmake-3.82_1

root@digoal:~/postgresql-9.3.4 # gmake world
...............
root@digoal:~/postgresql-9.3.4 # gmake install-world

创建pgsql的软链接
root@digoal:~/postgresql-9.3.4 # ln -s /opt/pgsql9.3.4 /opt/pgsql

配置资源限制, 参考
man login.conf
vi /etc/login.conf
default:\ 
        :passwd_format=sha512:\
        :copyright=/etc/COPYRIGHT:\
        :welcome=/etc/motd:\
        :setenv=MAIL=/var/mail/$,BLOCKSIZE=K:\
        :path=/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin ~/bin:\
        :nologin=/var/run/nologin:\
        :cputime=unlimited:\
        :datasize=unlimited:\
        :stacksize=unlimited:\
        :memorylocked=64K:\
        :memoryuse=unlimited:\
        :filesize=unlimited:\
        :coredumpsize=unlimited:\
        :openfiles=unlimited:\
        :maxproc=unlimited:\
        :sbsize=unlimited:\
        :vmemoryuse=unlimited:\
        :swapuse=unlimited:\
        :pseudoterminals=unlimited:\
        :priority=0:\
        :ignoretime@:\
        :umask=022:
postgres:\
        :openfiles=unlimited:\
        :maxproc=unlimited:

更新库
cap_mkdb /etc/login.conf

查看当前的ulimit
root@digoal:~ # su - postgres
postgres@digoal:~ % ulimit -a
cpu time               (seconds, -t)  unlimited
file size           (512-blocks, -f)  unlimited
data seg size           (kbytes, -d)  33554432
stack size              (kbytes, -s)  524288
core file size      (512-blocks, -c)  unlimited
max memory size         (kbytes, -m)  unlimited
locked memory           (kbytes, -l)  64
max user processes              (-u)  6670
open files                      (-n)  58266
virtual mem size        (kbytes, -v)  unlimited
swap limit              (kbytes, -w)  unlimited
sbsize                   (bytes, -b)  unlimited
pseudo-terminals                (-p)  unlimited


配置内核参数, 
参考
http://www.postgresql.org/docs/9.4/static/kernel-resources.html
root@digoal:~/postgresql-9.3.4 # vi /etc/sysctl.conf
kern.ipc.shmall=131072
kern.ipc.shmmax=536870912
kern.ipc.shm_use_phys=1   锁定shared memory到内存

生效
root@digoal:~/postgresql-9.3.4 # sysctl -w -f /etc/sysctl.conf
kern.ipc.shmall: 131072 -> 131072
kern.ipc.shmmax: 536870912 -> 536870912
kern.ipc.shm_use_phys: 1 -> 1


修改几个启动时加载的内核参数, 这几个参数不能在sysctl.conf中动态配置.
root@digoal:~/postgresql-9.3.4 # vi /boot/loader.conf 
zfs_load="YES"
kern.ipc.semmni=256
kern.ipc.semmns=512
kern.ipc.semmnu=256

重启freebsd
确认这几个配置已经生效.
root@digoal:~ # sysctl -a|grep semmni
kern.ipc.semmni: 256
<118>sysctl: oid 'kern.ipc.semmni' is a read only tunable at line 12
<118>sysctl: oid 'kern.ipc.semmni' is a read only tunable at line 12

配置防火墙,  参见

添加普通用户, 用于运行postgresql
root@digoal:~/postgresql-9.3.4 # adduser 
Username: postgres
Full name: postgresql
Uid (Leave empty for default): 
Login group [postgres]: 
Login group is postgres. Invite postgres into other groups? []: 
Login class [default]: 
Shell (sh csh tcsh nologin) [sh]: csh  选择csh
Home directory [/home/postgres]: 
Home directory permissions (Leave empty for default): 
Use password-based authentication? [yes]: 
Use an empty password? (yes/no) [no]: 
Use a random password? (yes/no) [no]: 
Enter password: 
Enter password again: 
Lock out the account after creation? [no]: 
Username   : postgres
Password   : *****
Full Name  : postgresql
Uid        : 1001
Class      : 
Groups     : postgres 
Home       : /home/postgres
Home Mode  : 
Shell      : /bin/csh
Locked     : no
OK? (yes/no): yes
adduser: INFO: Successfully added (postgres) to the user database.
Add another user? (yes/no): no
Goodbye!

配置环境变量.
root@digoal:~/postgresql-9.3.4 # su - postgres
postgres@digoal:~ % vi .cshrc 
# $FreeBSD: release/10.0.0/share/skel/dot.cshrc 243893 2012-12-05 13:56:39Z eadler $
#
# .cshrc - csh resource script, read at beginning of execution by each shell
#
# see also csh(1), environ(7).
# more examples available at /usr/share/examples/csh/
#

alias h         history 25
alias j         jobs -l
alias la        ls -aF
alias lf        ls -FA
alias ll        ls -lAF

# A righteous umask
umask 22

set path = (/opt/pgsql/bin /sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)
setenv PGHOME /opt/pgsql 
setenv LD_LIBRARY_PATH /opt/pgsql/lib
setenv PGDATA /home/postgres/pg_root
setenv PGHOST $PGDATA
setenv PGPORT 1921
setenv PGUSER postgres
setenv PGDATABASE postgres
setenv MANPATH $PGHOME/share/man

setenv  EDITOR  vi
setenv  PAGER   more
setenv  BLOCKSIZE       K

if ($?prompt) then
        # An interactive shell -- set some stuff up
        set prompt = "%N@%m:%~ %# "
        set promptchars = "%#"

        set filec
        set history = 1000
        set savehist = (1000 merge)
        set autolist = ambiguous
        # Use history to aid expansion
        set autoexpand
        set autorehash
        set mail = (/var/mail/$USER)
        if ( $?tcsh ) then
                bindkey "^W" backward-delete-word
                bindkey -k up history-search-backward
                bindkey -k down history-search-forward
        endif

endif

重新登录csh, 确认环境变量已经生效.
postgres@digoal:~ % exit
logout
root@digoal:~/postgresql-9.3.4 # su - postgres
postgres@digoal:~ % psql -V
psql (PostgreSQL) 9.3.4
postgres@digoal:~ % which psql
/opt/pgsql/bin/psql

初始化数据库集群
postgres@digoal:~ % initdb -D $PGDATA -U postgres -E UTF8 --locale=C -W
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 locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /home/postgres/pg_root ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
creating template1 database in /home/postgres/pg_root/base/1 ... ok
initializing pg_authid ... ok
Enter new superuser password: 
Enter it again: 
setting password ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
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:

    postgres -D /home/postgres/pg_root
or
    pg_ctl -D /home/postgres/pg_root -l logfile start

配置数据库参考
cd $PGDATA
vi postgresql.conf
listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
port = 1921                             # (change requires restart)
max_connections = 100                   # (change requires restart)
superuser_reserved_connections = 13     # (change requires restart)
unix_socket_directories = '.'   # comma-separated list of directories
unix_socket_permissions = 0700          # begin with 0 to use octal notation
tcp_keepalives_idle = 60                # TCP_KEEPIDLE, in seconds;
tcp_keepalives_interval = 10            # TCP_KEEPINTVL, in seconds;
tcp_keepalives_count = 10               # TCP_KEEPCNT;
shared_buffers = 128MB                  # min 128kB
maintenance_work_mem = 128MB            # min 1MB
shared_preload_libraries = 'pg_stat_statements'         # (change requires restart)
vacuum_cost_delay = 10                  # 0-100 milliseconds
vacuum_cost_limit = 10000               # 1-10000 credits
bgwriter_delay = 10ms                   # 10-10000ms between rounds
wal_level = hot_standby                 # minimal, archive, or hot_standby
synchronous_commit = off                # synchronization level;
wal_buffers = 16384kB                   # min 32kB, -1 sets based on shared_buffers
wal_writer_delay = 10ms         # 1-10000 milliseconds
checkpoint_segments = 8         # in logfile segments, min 1, 16MB each
archive_mode = on               # allows archiving to be done
archive_command = '/bin/date'           # command to use to archive a logfile segment
max_wal_senders = 8             # max number of walsender processes
wal_keep_segments = 32          # in logfile segments, 16MB each; 0 disables
hot_standby = on                        # "on" allows queries during recovery
max_standby_archive_delay = 300s        # max delay before canceling queries
max_standby_streaming_delay = 300s      # max delay before canceling queries
wal_receiver_status_interval = 1s       # send replies at least this often
hot_standby_feedback = on               # send info from standby to prevent
effective_cache_size = 2048MB
log_destination = 'csvlog'              # Valid values are combinations of
logging_collector = on          # Enable capturing of stderr and csvlog
log_directory = 'pg_log'                # directory where log files are written,
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
log_file_mode = 0600                    # creation mode for log files,
log_truncate_on_rotation = on           # If on, an existing log file with the
log_rotation_age = 1d                   # Automatic rotation of logfiles will
log_rotation_size = 10MB                # Automatic rotation of logfiles will
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose           # terse, default, or verbose messages
log_lock_waits = on                     # log lock waits >= deadlock_timeout
log_statement = 'ddl'                   # none, ddl, mod, all
log_timezone = 'PRC'
autovacuum = on                 # Enable autovacuum subprocess?  'on'
log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'C'                       # locale for system error message
lc_monetary = 'C'                       # locale for monetary formatting
lc_numeric = 'C'                        # locale for number formatting
lc_time = 'C'                           # locale for time formatting
default_text_search_config = 'pg_catalog.english'
pg_stat_statements.max=1000
pg_stat_statements.track=all

vi pg_hba.conf
host all all 0.0.0.0/0 md5


启动数据库
pg_ctl start
测试
postgres@digoal:~/pg_root % psql
psql (9.3.4)
Type "help" for help.
No relations found.
postgres=# create extension pg_stat_statements;
CREATE EXTENSION



安装pgfincore, 报错, 暂未解决.
可能freebsd 10的libc版本太老.
http://git.postgresql.org/gitweb/?p=pgfincore.git;a=snapshot;h=0c891865d0e3777faafadb2fe3d84748b3a77d87;sf=tgz
root@digoal:~ # pkg install lrzsz-0.12.20_4
root@digoal:~ # lrz
# tar -zxvf pgfincore-0c89186.tar.gz
# mv pgfincore-0c89186 postgresql-9.3.4/contrib/
root@digoal:~ # mv pgfincore-0c89186 postgresql-9.3.4/contrib/
root@digoal:~ # cd postgresql-9.3.4/contrib/pgfincore-0c89186/
root@digoal:~/postgresql-9.3.4/contrib/pgfincore-0c89186 # set path = (/opt/pgsql/bin /sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)
root@digoal:~/postgresql-9.3.4/contrib/pgfincore-0c89186 # which pg_config
/opt/pgsql/bin/pg_config

gmake uninstall
gmake
In file included from pgfincore.c:18:
./fincore.h:10:30: error: unknown type name 'loff_t'; did you mean 'off_t'?
int fincore(unsigned int fd, loff_t start, loff_t len, unsigned char * vec);
                             ^~~~~~
                             off_t
/usr/include/sys/stat.h:86:18: note: 'off_t' declared here
typedef __off_t         off_t;
                        ^
In file included from pgfincore.c:18:
./fincore.h:10:44: error: unknown type name 'loff_t'; did you mean 'off_t'?
int fincore(unsigned int fd, loff_t start, loff_t len, unsigned char * vec);
                                           ^~~~~~
                                           off_t
/usr/include/sys/stat.h:86:18: note: 'off_t' declared here
typedef __off_t         off_t;
                        ^
In file included from pgfincore.c:18:
./fincore.h:12:30: error: unknown type name 'loff_t'; did you mean 'off_t'?
int fincore(unsigned int fd, loff_t start, loff_t len, unsigned char * vec)
                             ^~~~~~
                             off_t
/usr/include/sys/stat.h:86:18: note: 'off_t' declared here
typedef __off_t         off_t;
                        ^
In file included from pgfincore.c:18:
./fincore.h:12:44: error: unknown type name 'loff_t'; did you mean 'off_t'?
int fincore(unsigned int fd, loff_t start, loff_t len, unsigned char * vec)
                                           ^~~~~~
                                           off_t
/usr/include/sys/stat.h:86:18: note: 'off_t' declared here
typedef __off_t         off_t;
                        ^
In file included from pgfincore.c:18:
./fincore.h:14:10: warning: implicit declaration of function 'syscall' is invalid in C99 [-Wimplicit-function-declaration]
  return syscall(__NR_fincore, fd, start, len, vec);
         ^
pgfincore.c:245:36: error: use of undeclared identifier '_SC_AVPHYS_PAGES'
        values[1] = Int64GetDatum(sysconf(_SC_AVPHYS_PAGES));
                                          ^
/opt/pgsql9.3.4/include/server/postgres.h:555:47: note: expanded from macro 'Int64GetDatum'
#define Int64GetDatum(X) ((Datum) SET_8_BYTES(X))
                                              ^
/opt/pgsql9.3.4/include/server/postgres.h:318:38: note: expanded from macro 'SET_8_BYTES'
#define SET_8_BYTES(value)      ((Datum) (value))
                                          ^
pgfincore.c:353:29: error: use of undeclared identifier '_SC_AVPHYS_PAGES'
        pgfdv->pagesFree = sysconf(_SC_AVPHYS_PAGES);
                                   ^
pgfincore.c:634:33: error: use of undeclared identifier '_SC_AVPHYS_PAGES'
        pgfloader->pagesFree = sysconf(_SC_AVPHYS_PAGES);
                                       ^
pgfincore.c:951:30: error: use of undeclared identifier '_SC_AVPHYS_PAGES'
        pgfncr->pagesFree = sysconf(_SC_AVPHYS_PAGES);
                                    ^
1 warning and 8 errors generated.
gmake: *** [pgfincore.o] Error 1

查过man  sysconf后, 发现这里的identifier确实没有_SC_AVPHYS_PAGES.
_SC_AVPHYS_PAGES
只有
     _SC_PAGESIZE
             The size of a system page in bytes.

     _SC_PAGE_SIZE
             Equivalent to _SC_PAGESIZE.
     _SC_PHYS_PAGES
             The number of pages of physical memory.  Note that it is possible
             that the product of this value and the value of _SC_PAGESIZE will
             overflow a long in some configurations on a 32bit machine.

在CentOS 6.x中当然有这个定义
man sysconf
        - _SC_AVPHYS_PAGES
              The number of currently available pages of physical memory.
在opensolaris中也有这个定义
man sysconf


或者玩一下opensolaris (or illumos now), 不过oracle关闭了opensolaris, 现已由illumos社区继承.
This is the home of the illumos project, the open source fork of Sun's OpenSolaris. Launched in 2010, the project enjoys financial and technical support from several key companies which rely on the illumos kernel as the technological foundation for their own products, as well as the backing of a growing developer community.

[参考]
6. man login.conf
7.  man  sysconf
8. freebsd的一些基本操作, 收集自互联网.
Step 1 FreeBSD下安装软件包

·pkg_add方式安装软件包。

·Ports方式安装软件包。

·源代码方式安装软件包。

跟随下面步骤进行远程软件包的安装.
FreeBSD下可以使用多种方法安装软件包,即可以使用pkg_add方式安装,也可以使用Ports方式安装,还可以下程序源码进行编译安装。不同的安装方式有不向的特点,有的省时间、快速;有点定制性高可以根据自己需要进行选择。

pkg_add方式安装

pkg_add方式安装方法1、用户预先下载编译好的需要进行安装的软件包,一般是*.tbz 和*.tgz格式的软件包。直接使用pkg_add+软件包名称,安装即可。可以使用-v参数显示安装过程。源程序可以从各大FTP站点(比如ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/packages/)或各发行光盘中找到。

pkg_add+软件包名称安装。此处以安装lsof-4.79D.tbz为例

下载并显示lsof-4.79D.tbz软件包

进行安装pkg_add -v lsof-4.79D.tbz (参数-v显示安装过程信息),安装完成可以输入rehash刷新。

pkg_add方式安装方法2、用户不需要下载软件包,直接在安装时输入pkg_add -r和软件包的名称,系统将自动查找并下载、安装软件包(参数-r的作用是,使系统自动决定软件包的版本,格式并进行下载软件安装)。用户设定PACKAGESITE
这个环境变量,选择距离最近的FTP服务器,可以加速下载安装过程。在上一篇的用户shell中PACKAGESITE
这个环境变量,此处不再设定。

pkg_add是在freebsd里面用来通过package安装软件一个命令,而-r 则是说本地没有package包,安装时自动从网上下载。默认的站点一般如:
ftp://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-6.1-release/All/
这样的格式,访问国外的服务器一般比国内的要慢很多,特别是现在正值海底光纤就震断之际,国外网站更是难以访问。所以非常有必要修改这个默认ftp站点,所以就要设置PACKAGESITE这个环境变量

在csh shell里这样改:

#setenv PACKAGESITE ftp://ftp.freebsd.org.cn/pub/FreeBSD/ports/i386/packages-6.1-release/All/

注意在不同的shell里设置环境变量的方法是不同的,在不同的shell运行这个,不会提示错误,但设置后也没有效果的。
在sh shell里这样改:

# export PACKAGESITE=ftp://ftp.freebsd.org.cn /pub/FreeBSD/ports/i386/packages-6.1-release/All/

好了现在通过pkg_add -r 来安装软件下载快得多了!
pkg_add -r+软件包名称安装(系统自动解决依赖关系)。此处以安装wget为例

不需要指定版本及格式,直接输入安装命令pkg_add -r wget即可。系统会自动连接服务器进行下载,并且自动解决依赖关系。



使用Ports方式安装软件包

Ports中进入软件包名称安装目录,执行make install clean进行安装(系统自动解决依赖关系) Ports安装命令总结

make install :安裝所选定的 Port

make deinstall :移除已安裝的 Port

make reinstall :移除之后再安裝一次

make clean :安裝完毕后移除 make 資料夾下的档案

make distclean :移除安裝后的原始档案,以节省磁碟空间

make rmconfig :移除之前安裝过程中选项的設定



使用源代码进行安装程序

以Apache22为例(假设安装路径为/usr/local/apache22)

 下载Apache22的talball可以使用wget在FreeBSD下载,也可以在Win下载完成再传到FreeBSD机器上。

解包并指定安装路径,进行配置,安装

--prefix : 設定 Apache httpd 安裝路径

--enable-so : 告訴 Apache 启动 so 模組, 让 Apache 能够载入 shared modules(例如 PHP)

 启动Apache22进行测试

浏览器打开http://youipaddress



好了,Apache22安装成功了。




Step 2 FreeBSD下的下载程序.

FreeBSD中安装下载程序axel和wget:
安装axel程序

可以使用任意方法安装,如pkg_add或者Ports方式安装。这里使用pkg_add -r axel命令安装

axel程序的使用,axel+要下载的文件的url

安装wget程序

使用Ports方式安装,查找到wget所在目录,进入目录,执行make install clean

使用方式和axel基本一样。wget+下载的程序名称的url

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
7月前
|
Unix Linux
Linux/FreeBSD 安装
在Linux/FreeBSD上安装Julia 1.7.2的步骤:从[官方](https://julialang-s3.julialang.org/bin/linux/x64/1.7/julia-1.7.2-linux-x86_64.tar.gz)或[Tsinghua镜像](https://mirrors.tuna.tsinghua.edu.cn/julia-releases/bin/linux/x86/1.7/julia-1.7.2-linux-i686.tar.gz)下载二进制包,然后使用`tar`解压。
|
Shell Unix 域名解析
|
数据安全/隐私保护 网络协议 Unix