1. 官网下载软件
地址链接: https://www.postgresql.org/ftp/source/v10.0/
postgreSQL 的 版本是 10 操作系统是 Red Hat Enterprise Linux Server release 7.2 (Maipo)

[root@tjtestrac1 postgreSQL]# wget https://ftp.postgresql.org/pub/source/v10.0/postgresql-10.0.tar.gz
--2018-12-07 10:49:07-- https://ftp.postgresql.org/pub/source/v10.0/postgresql-10.0.tar.gz
Resolving ftp.postgresql.org (ftp.postgresql.org)... 72.32.157.246, 87.238.57.227, 204.145.124.244, ...
Connecting to ftp.postgresql.org (ftp.postgresql.org)|72.32.157.246|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25830653 (25M) [application/x-gzip]
Saving to: ‘postgresql-10.0.tar.gz’
100%[==================================================================================================================================>] 25,830,653 1.22MB/s in 31s
2018-12-07 10:49:47 (803 KB/s) - ‘postgresql-10.0.tar.gz’ saved [25830653/25830653]
2. 安装postgreSQL 10
解压软件包
[root@tjtestrac1 postgreSQL]# tar -xvf postgresql-10.0.tar.gz
安装依赖的RPM包
[root@tjtestrac1 postgreSQL]# yum groupinstall "Development tools"
[root@tjtestrac1 postgreSQL]# yum install -y bison flex readline-devel zlib-devel
在源代码目录中查看编译选项
[root@tjtestrac1 postgreSQL]# cd postgresql-10.0
[root@tjtestrac1 postgresql-10.0]# ./configure --help
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local/pgsql]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/pgsql/bin', `/usr/local/pgsql/lib' etc. You can specify
an installation prefix other than `/usr/local/pgsql' using `--prefix',
for instance `--prefix=$HOME'.
...
postgreSQL 编译选项众多,常用的参数如下:
| 参数 | 编译选项说明 |
|---|---|
| –prefix=PREFIX | 指定安装目录,默认的安装目录为/usr/local/pgsql |
| –with-pgport=PORTNUM | 修改默认的端口号5432,端口可以再安装之后修改 |
| –with-blocksize=BLOCKSIZE | 指定数据文件的block块大小,默认是8K 适用于OLTP系统,如果是OLAP系统建议适当增加到16K,32K甚至更大 |
| –with-segsize=SEGSIZE | 指定单个数据文件的大小,默认为1GB |
| –with-wal-blocksize | 指定WAL文件块的大小,默认是8KB |
| –with-wal-segsize | 指定WAL文件的大小,默认是16MB |
********注意 --with-xxx-size 这几个参数 只能在编译的时候指定
创建PG的安装路径PG_HOME
[root@tjtestrac1 pg10]# mkdir -p /u02/postgreSQL/pg10
编译PG 软件到 PG_HOME
[root@tjtestrac1 postgresql-10.0]# ./configure --prefix=/u02/postgreSQL/pg10 --with-pgport=1985 --with-segsize=16 --with-wal-segsize=256
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 1985
checking for block size... 8kB
checking for segment size... 16GB
checking for WAL block size... 8kB
checking for WAL segment size... 256MB
checking for gcc... gcc
checking whether the C compiler works... yes
...
...
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
编译安装
[root@tjtestrac1 postgresql-10.0]# gmake
......
......
gmake -C config all
gmake[1]: Entering directory `/u02/postgreSQL/postgresql-10.0/config'
gmake[1]: Nothing to be done for `all'.
gmake[1]: Leaving directory `/u02/postgreSQL/postgresql-10.0/config'
All of PostgreSQL successfully made. Ready to install.
Ready to install 提示一切正常 可以安装了
[root@tjtestrac1 postgreSQL]# gmake install
...
...
gmake[1]: Entering directory `/u02/postgreSQL/postgresql-10.0/config'
/bin/mkdir -p '/u02/postgreSQL/pg10/lib/postgresql/pgxs/config'
/bin/install -c -m 755 ./install-sh '/u02/postgreSQL/pg10/lib/postgresql/pgxs/config/install-sh'
/bin/install -c -m 755 ./missing '/u02/postgreSQL/pg10/lib/postgresql/pgxs/config/missing'
gmake[1]: Leaving directory `/u02/postgreSQL/postgresql-10.0/config'
PostgreSQL installation complete.
查看安装的PG的版本
[root@tjtestrac1 bin]# /u02/postgreSQL/pg10/bin/postgres --version
postgres (PostgreSQL) 10.0
查看PG_HOME的文件路径
[root@tjtestrac1 pg10]# tree -L 1 /u02/postgreSQL/pg10/
/u02/postgreSQL/pg10/
├── bin -- 这个文件夹里面是应用程序:分为客户端和应用端
├── include--这个文件夹是C,C++的头文件
├── lib --编译文件
└── share --PostgreSQL的文档,man, 示例文件
4 directories, 0 files
客户端程序和服务器端程序
- 客户端
- clusterdb,reindexdb,vacuumdb,vacuumlo,createdb,dropdb,createuser,dropuser,pg_backup,pg_dump,pg_restore
- 服务器端
- initdb,pg_archivecleanup,pg_controldata,pg_ctl,pg_resetwal,pg_rewind,pg_upgrade,postgres
3. 初始化数据库
在初始化数据库之前,建议在linux下创建用户postgres
[root@tjtestrac1 lib]# groupadd -g 2000 postgres
[root@tjtestrac1 lib]# useradd -g 2000 -u 2000 postgres
[root@tjtestrac1 lib]# id postgres
uid=2000(postgres) gid=2000(postgres) groups=2000(postgres)
创建新的数据库目录
| path | remark |
|---|---|
| /u02/postgreSQL/db10/data | 数据文件路径 |
| /u02/postgreSQL/db10/backups | 文件备份路径 |
| /u02/postgreSQL/db10/scripts | 监控脚本路径 |
| /u02/postgreSQL/db10/archive_wals | WAL日志文件的路径 |
[root@tjtestrac1 postgreSQL]# mkdir -p /u02/postgreSQL/db10/{data,backups,scripts,archive_wals}
修改数据目录的权限
[root@tjtestrac1 postgreSQL]# chown -R postgres.postgres /u02/postgreSQL/db10
[root@tjtestrac1 postgreSQL]# chmod 0700 /u02/postgreSQL/db10/data/
创建数据库: initdb 命令创建数据库: template1 & postgres
切换用户到 postgres
[root@tjtestrac1 postgreSQL]# su - postgres
[postgres@tjtestrac1 ~]$ /u02/postgreSQL/pg10/bin/initdb --help
initdb initializes a PostgreSQL database cluster.
Usage:
initdb [OPTION]... [DATADIR]
Options:
-A, --auth=METHOD default authentication method for local connections
--auth-host=METHOD default authentication method for local TCP/IP connections
--auth-local=METHOD default authentication method for local-socket connections
[-D, --pgdata=]DATADIR location for this database cluster
-E, --encoding=ENCODING set default encoding for new databases
--locale=LOCALE set default locale for new databases
--lc-collate=, --lc-ctype=, --lc-messages=LOCALE
--lc-monetary=, --lc-numeric=, --lc-time=LOCALE
set default locale in the respective category for
new databases (default taken from environment)
--no-locale equivalent to --locale=C
--pwfile=FILE read password for the new superuser from file
-T, --text-search-config=CFG
default text search configuration
-U, --username=NAME database superuser name
-W, --pwprompt prompt for a password for the new superuser
-X, --waldir=WALDIR location for the write-ahead log directory
Less commonly used options:
-d, --debug generate lots of debugging output
-k, --data-checksums use data page checksums
-L DIRECTORY where to find the input files
-n, --no-clean do not clean up after errors
-N, --no-sync do not wait for changes to be written safely to disk
-s, --show show internal settings
-S, --sync-only only sync data directory
Other options:
-V, --version output version information, then exit
-?, --help show this help, then exit
help 里面列出了具体参数的详情解释,这里就不赘述了
[postgres@tjtestrac1 ~]$ /u02/postgreSQL/pg10/bin/initdb -D /u02/postgreSQL/db10/data/ -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 "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /u02/postgreSQL/db10/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... 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:
/u02/postgreSQL/pg10/bin/pg_ctl -D /u02/postgreSQL/db10/data/ -l logfile start
启动数据库
[postgres@tjtestrac1 ~]$ /u02/postgreSQL/pg10/bin/pg_ctl -D /u02/postgreSQL/db10/data/ -l logfile start
waiting for server to start................. done
server started
查看数据库状态
[postgres@tjtestrac1 ~]$ /u02/postgreSQL/pg10/bin/pg_ctl -D /u02/postgreSQL/db10/data/ status
pg_ctl: server is running (PID: 27289)
/u02/postgreSQL/pg10/bin/postgres "-D" "/u02/postgreSQL/db10/data"
关闭数据库
[postgres@tjtestrac1 ~]$ /u02/postgreSQL/pg10/bin/pg_ctl -D /u02/postgreSQL/db10/data/ -ms stop
waiting for server to shut down.... done
server stopped
版权声明:本文为chenxu_0209原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。