您的位置:首页 > 运维架构 > Linux

postgresql 9.5.1 on centos 6.6

2016-02-16 17:20 573 查看
*****************************************************
1.编译安装
*****************************************************

/*******

make --version

gcc --version

perl --version

python --version

----1.安装环境包

yum -y install wget gcc gcc-c++ readline-devel zlib-devel make   systemtap systemtap-sdt-devel \
perl perl-devel python python-devel tcl  tcl-devel    perl-ExtUtils-Embed \
sgml-common docbook stylesheets openjade  sgml-tools  xsltproc libxslt libxslt-devel \
libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel   pam pam-devel bison flex libreadline6-devel

******/

----2.编译安装

# wget https://ftp.postgresql.org/pub/source/v9.5.1/ 
#mkdir -p /soft && cd /soft

#rz

# tar xf postgresql-9.5.1.tar.gz && cd postgresql-9.5.1

#./configure

./configure --prefix=/data/pg \
--with-pgport=5432  \
--with-perl --with-python --with-tcl  \
--with-openssl  --without-ldap  \
--with-libxml  --with-libxslt    \
--enable-thread-safety    \
--with-wal-blocksize=64   \
--with-blocksize=32 \
--with-wal-segsize=64 \
-enable-dtrace  \
--enable-debug

#make && make install

#useradd postgres && echo 'pg01!@#' |passwd --stdin  postgres

#mkdir /data/pg/data -p  && chown -R postgres /data/pg/data

# su postgres

$ vi  ~/.bash_profile

# postgres
#PostgreSQL端口
PGPORT=5432

#PostgreSQL数据目录
PGDATA=/data/pg/data

export PGPORT PGDATA

#所使用的语言
export LANG=en_US.utf8

#PostgreSQL 安装目录
export PGHOME=/data/pg

#PostgreSQL 连接库文件
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH

export DATE=`date +"%Y%m%d%H%M"`

#将PostgreSQL的命令行添加到 PATH 环境变量
export PATH=$PGHOME/bin:$PATH

#PostgreSQL的 man 手册
export MANPATH=$PGHOME/share/man:$MANPATH

#PostgreSQL的默认用户
export PGUSER=postgres

#PostgreSQL默认主机地址
export PGHOST=127.0.0.1

#默认的数据库名
export PGDATABASE=postgres

#source ~/.bash_profile

#echo $PATH

#执行数据库初始化脚本
$/data/pg/bin/initdb --encoding=utf8 -D /data/pg/data

---结果如下
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.utf8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/pg/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
creating template1 database in /data/pg/data/base/1 ... ok
initializing pg_authid ... 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:

/data/pg/bin/postgres -D /data/pg/data
or
/data/pg/bin/pg_ctl -D /data/pg/data -l logfile start

$退出变更登录
exit

---3.复制PostgreSQL执行脚本
cp /soft/postgresql-9.5.1/contrib/start-scripts/linux  /etc/init.d/postgresql

#增加执行权限
chmod +x /etc/init.d/postgresql

---4.编辑PostgreSQL执行脚本,确定以下参数或修改

vi /etc/init.d/postgresql

# Installation prefix
prefix=/data/pg

# Data directory
PGDATA="/data/pg/data"

# Who to run the postmaster as, usually "postgres".  (NOT "root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$PGDATA/serverlog"

----5.编辑pg参数配置文件(参见配置文件)

#vi /data/pg/data/postgresql.conf
59 listen_addresses = '*'

----6.启动或停止PG

# netstat  -nlp | grep :5432
tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      4733/postgres

# kill 4733

$pg_ctl start

$pg_ctl stop

# 设置开机自动启动服务
chkconfig postgresql on

*****************************************************
2.数据库配置
*****************************************************

--2.1 设置远程登录数据库

#vi /data/pg/data/postgresql.conf

# -----------------------------
# PostgreSQL configuration file
# -----------------------------
#
# This file consists of lines of the form:
#
#   name = value
#

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -
listen_addresses = '*'		# what IP address(es) to listen on;
port = 5432			# (change requires restart)
max_connections = 100			# (change requires restart)
superuser_reserved_connections = 3	# (change requires restart)

# - Security and Authentication -
password_encryption = on

#------------------------------------------------------------------------------
# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------

# - Memory -
shared_buffers = 3GB			# min 128kB, recommend Physical memory 25%
work_mem = 4MB				# min 64kB
maintenance_work_mem = 1GB            # min 1MB
dynamic_shared_memory_type = posix	# the default is the first option

# - Cost-Based Vacuum Delay -
vacuum_cost_delay = 10                 # 0-100 milliseconds
vacuum_cost_limit = 200                # 1-10000 credits

# - Background Writer -
bgwriter_delay = 10ms			# 10-10000ms between rounds

#------------------------------------------------------------------------------
# WRITE AHEAD LOG
#------------------------------------------------------------------------------
# - Settings -
fsync = on                      ## turns forced synchronization on or off,Close to improve performance
synchronous_commit = on         ## synchronization level,Close to improve performance
wal_sync_method = fdatasync		# the default is the first option
full_page_writes = on                  # recover from partial page writes
wal_buffers = 16MB 			# min 32kB, -1 sets based on shared_buffers
wal_writer_delay = 10ms		# 1-10000 milliseconds,Effective when synchronous_commit set close

#group commit
commit_delay = 0                       # range 0-100000, in microseconds
commit_siblings = 5                    # range 1-1000

# - Checkpoints -
checkpoint_timeout = 300s		# range 30s-1h
checkpoint_completion_target = 0.9	# checkpoint target duration, 0.0 - 1.0

#------------------------------------------------------------------------------
# QUERY TUNING
#------------------------------------------------------------------------------

# - Planner Method Configuration -
enable_bitmapscan = on
enable_hashagg = on
enable_hashjoin = on
enable_indexscan = on
enable_indexonlyscan = on
#enable_material = on
#enable_mergejoin = on
#enable_nestloop = on
enable_seqscan = off
#enable_sort = on
enable_tidscan = off

# - Planner Cost Constants -

#seq_page_cost = 1.0                    # measured on an arbitrary scale
#random_page_cost = 4.0                 # same scale as above
#cpu_tuple_cost = 0.01                  # same scale as above
#cpu_index_tuple_cost = 0.005           # same scale as above
#cpu_operator_cost = 0.0025             # same scale as above
effective_cache_size = 12GB             #recommend Physical memory 75%

# - Genetic Query Optimizer -

#geqo = on
#geqo_threshold = 12
#geqo_effort = 5                        # range 1-10
#geqo_pool_size = 0                     # selects default based on effort
#geqo_generations = 0                   # selects default based on effort
#geqo_selection_bias = 2.0              # range 1.5-2.0
#geqo_seed = 0.0                        # range 0.0-1.0

# - Other Planner Options -

#default_statistics_target = 100        # range 1-10000
#constraint_exclusion = partition       # on, off, or partition
#cursor_tuple_fraction = 0.1            # range 0.0-1.0
#from_collapse_limit = 8
#join_collapse_limit = 8                # 1 disables collapsing of explicit
# JOIN clauses

#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -
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_truncate_on_rotation = off		# If on, an existing log file with the
log_rotation_age = 0			# Automatic rotation of logfiles will
log_rotation_size = 50MB		# Automatic rotation of logfiles will
log_min_duration_statement = 2000ms	# -1 is disabled, 0 logs all statements

# - What to Log -
log_checkpoints = on
log_lock_waits = on			# log lock waits >= deadlock_timeout
log_statement = 'ddl'			# none, ddl, mod, all
log_timezone = 'PRC'

#------------------------------------------------------------------------------
# RUNTIME STATISTICS
#------------------------------------------------------------------------------

# - Query/Index Statistics Collector -
track_activity_query_size = 2048	# (change requires restart)

#------------------------------------------------------------------------------
# AUTOVACUUM PARAMETERS
#------------------------------------------------------------------------------
autovacuum = on			# Enable autovacuum subprocess?  'on'
log_autovacuum_min_duration = 0	# -1 disables, 0 logs all actions and
autovacuum_max_workers = 3	# max number of autovacuum subprocesses
autovacuum_vacuum_cost_delay = 0     # default vacuum cost delay for autovacuum, in milliseconds, -1 means use vacuum_cost_delay

#------------------------------------------------------------------------------
# CLIENT CONNECTION DEFAULTS
#------------------------------------------------------------------------------
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'en_US.utf8'			# locale for system error message
lc_monetary = 'en_US.utf8'			# locale for monetary formatting
lc_numeric = 'en_US.utf8'			# locale for number formatting
lc_time = 'en_US.utf8'				# locale for time formatting
default_text_search_config = 'pg_catalog.english'

#------------------------------------------------------------------------------
# LOCK MANAGEMENT
#------------------------------------------------------------------------------
deadlock_timeout = 1s

重启数据库.

pg_ctl -m "fast" restart

--2.2 以postgres用户登录数据库,修改postgres用户的数据库密码
psql -U postgres
postgres=# ALTER USER postgres PASSWORD 'passwd123!';
postgres=# \q

--开启登录数据库密码认证

#vi /data/pg/data/pg_hba.conf

#to allow your client visiting postgresql server
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5

psql -h 192.168.50.188  -d postgres -U postgres

重启数据库.

pg_ctl -m "fast" restart

--2.3 远程连接
psql -h 192.168.50.188  -d postgres -U postgres
Password for user postgres:

psql -h 192.168.50.188  -d postgres -U postgres -W

psql -h 192.168.50.188  -d postgres -U postgres

Connection options:
-h, --host=HOSTNAME      database server host or socket directory (default: "127.0.0.1")
-p, --port=PORT          database server port (default: "5432")
-U, --username=USERNAME  database user name (default: "postgres")
-w, --no-password        never prompt for password
-W, --password

*****************************************************
3.环境变量
*****************************************************
---postgres用户

--设置用户自动登录

$vi ~/.bash_profile

export PGPASSWORD="passwd123!"
alias psqla="psql -h 192.168.50.188  -d postgres -U postgres -W"

$source ~/.bash_profile

---root用户

vi ~/.bash_profile

alias sudop="su - postgres"

source ~/.bash_profile
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  postgresql9.5.1