linux下的PGSQL安装步骤
在LINUX下关闭防⽕墙
service iptables stop
1.下载安装包
从Postgres官⽅⽹站下载postgresql-9.6.安装包
2.创建⽤户
创建Postgres⽤户:
useradd postgres
passwd postgres
输⼊设置的密码
3.解压
为了保证我们使⽤postgres⽤户安装完成后其他⽤户也能使⽤,我们采⽤root⽤户解压安装包到/usr⽬录中,再将相应⽬录的权限改回postgres
su
cd /data/soft/postgres/
tar -zxvf postgresql-9.4. -C /usr
4.更改⽬录权限
切换到/usr⽬录,到刚才解压的pgsql⽂件夹,将pasql⽂件夹的所有者改回postgres
chown -R  /usr/pgsql
5.建⽴数据⽬录
在/opt⽬录下建⽴postgres的数据⽬录,并更改⽂件夹所有者为postgres
cd /opt
mkdir postgres
cd postgres
mkdir 9.4
cd 9.4
mkdir data
cd /opt
chown -R  /opt/postgres
如果后期忘记了posgresql安装到什么⽬录了,可以通过查f,来定位postgresql的位置
添加PG_HOME和PGDATA环境变量
vi /etc/profile
export PG_HOME=/usr/pgsql
export PGDATA=/opt/postgres/9.4/data
export PATH=$PATH:$PG_HOME/bin
⽣效命令
source /etc/profile
6.初始化数据库
切换到postgres⽤户,初始化数据库
find . -name initdb
su - postgres
/usr/pgsql/bin/initdb -D /opt/postgres/9.4/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.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.
fixing permissions on existing directory /opt/postgres/9.6/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 f 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/pgsql/bin/pg_ctl -D /opt/postgres/9.6/data -l logfile start
如果想对postgres进⾏配置,可以编辑如下⽂件:
cd /opt/postgres/9.4/data
f
7.启动数据库
先创建⽇志⽬录
cd /opt/postgres/9.4/
mkdir logs
chown -R  /opt/postgres/9.4/logs
启动数据库:
su - postgres
/usr/pgsql/bin/pg_ctl -D /opt/postgres/9.4/data -l /opt/postgres/9.4/logs/postgres.log start
查看服务是否启动成功
ps -ef | grep postgres
当看到类似下⾯的结果时,说明服务启动成功了
[postgres@hadoop1 9.6]$ ps -ef | grep postgres
root 3659 3644 0 09:46 pts/1 00:00:00 su - postgres
postgres 3660 3659 0 09:46 pts/1 00:00:00 -bash
root 4056 3751 0 10:15 pts/1 00:00:00 su - postgres
postgres 4057 4056 0 10:15 pts/1 00:00:00 -bash
postgres 4946 1 0 10:26 pts/1 00:00:00 /usr/pgsql/bin/postgres -D /opt/postgres/9.6/data
postgres 4948 4946 0 10:26 ? 00:00:00 postgres: checkpointer process
postgres 4949 4946 0 10:26 ? 00:00:00 postgres: writer process
postgres 4950 4946 0 10:26 ? 00:00:00 postgres: wal writer process
postgres 4951 4946 0 10:26 ? 00:00:00 postgres: autovacuum launcher process
postgres 4952 4946 0 10:26 ? 00:00:00 postgres: stats collector process
postgres 5560 4057 0 10:30 pts/1 00:00:00 ps -ef
postgres 5561 4057 0 10:30 pts/1 00:00:00 grep postgres
退出数据库,在root⽤户下调⽤如下命令查看postgres服务:
service --status-all | grep postgres
然后发现并没有结果,因此需要将postgresql注册到服务列表
8.修改postgres⽤户的访问密码并测试建库建表
PostgreSQL 数据库默认会创建⼀个postgres的数据库⽤户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这⾥设定为’postgres’
直接在控制台输⼊以下命令:
su - postgres
psql
# ALTER USER postgres WITH PASSWORD 'postgres';
# select * from pg_shadow ;
# create database project;
# \c project
project=# create table person(id integer, name text);
project=# insert into person values (1, 'jimmy');
project=# select * from person
可以看到我们刚才插⼊的那条数据
9.将postgresql-9.6添加到服务列表
su root
cd /etc/init.d/
touch postgresql-9.4
vi postgresql-9.4
添加以下内容:
#! /bin/sh
# chkconfig: 2345 98 02
linux安装数据库
# description: PostgreSQL RDBMS
# This is an example of a start/stop script for SysV-style init, such
# as is used on Linux systems. You should edit some of the variables
# and maybe the 'echo' commands.
#
# Place this file at /etc/init.d/postgresql (or
# /etc/rc.d/init.d/postgresql) and make symlinks to
# /etc/rc.d/rc0.d/K02postgresql
# /etc/rc.d/rc1.d/K02postgresql
# /etc/rc.d/rc2.d/K02postgresql
# /etc/rc.d/rc3.d/S98postgresql
# /etc/rc.d/rc4.d/S98postgresql
# /etc/rc.d/rc5.d/S98postgresql
# Or, if you have chkconfig, simply:
# chkconfig --add postgresql
#
# Proper init scripts on Linux systems normally require setting lock
# and pid files under /var/run as well as reacting to network
# settings, so you should treat this with care.
# Original author: Ryan Kirkpatrick <>
# contrib/start-scripts/linux
## EDIT FROM HERE
# Installation prefix
prefix=/usr/pgsql
# Data directory
PGDATA="/opt/postgres/9.4/data/"
# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres
# Where to keep a log file
PGLOG="/opt/postgres/9.4/logs/postgres.log"
# It's often a good idea to protect the postmaster from being killed by the
# OOM killer (which will tend to preferentially kill the postmaster because
# of the way it accounts for shared memory). To do that, uncomment these
# three lines:
#PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
#PG_MASTER_OOM_SCORE_ADJ=-1000
#PG_CHILD_OOM_SCORE_ADJ=0
# Older Linux kernels may not have /proc/self/oom_score_adj, but instead
# /proc/self/oom_adj, which works similarly except for having a different
# range of scores. For such a system, uncomment these three lines instead: #PG_OOM_ADJUST_FILE=/proc/self/oom_adj
#PG_MASTER_OOM_SCORE_ADJ=-17
#PG_CHILD_OOM_SCORE_ADJ=0
## STOP EDITING HERE
# The path that is to be used for the script
PATH=/usr/local/
# What to use to start up the postmaster. (If you want the script to wait
# until the server has started, you could use "pg_ctl start -w" here.
# But without -w, pg_ctl adds no value.)
DAEMON="$prefix/bin/postmaster"
# What to use to shut down the postmaster
PGCTL="$prefix/bin/pg_ctl"
set -e
# Only start if we can find the postmaster.
test -x $DAEMON ||
{
echo "$DAEMON not found"
if [ "$1" = "stop" ]
then exit 0
else exit 5
fi
}
# If we want to tell child processes to adjust their OOM scores, set up the
# necessary environment variables. Can't just export them through the "su". if [ -e "$PG_OOM_ADJUST_FILE" -a -n "$PG_CHILD_OOM_SCORE_ADJ" ] then
DAEMON_ENV="PG_OOM_ADJUST_FILE=$PG_OOM_ADJUST_FILE
PG_OOM_ADJUST_VALUE=$PG_CHILD_OOM_SCORE_ADJ"
fi
# Parse command line parameters.
case $1 in
start)
echo -n "Starting PostgreSQL: "
test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE" su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
echo "ok"
;;
stop)
echo -n "Stopping PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"
echo "ok"
;;
restart)
echo -n "Restarting PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"
test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE" su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
echo "ok"
;;
reload)
echo -n "Reload PostgreSQL: "
su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
echo "ok"
;;
status)
su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
;;
*)
# Print help
echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
exit 1
;;
esac
exit 0
需要根据安装路径修改配置中相应的路径信息
接着就可以使⽤service postgresql-9.6 start/stop/status来操作postgresql的启停了
10.把postgresql加⼊⾃启动列表
cd /etc/init.d
chkconfig --add postgresql-9.4
查看⼀下⾃启动列表
chkconfig --list
在这⾥可以看到postgresq-9.6已经在其中了。
11.配置postgresql允许远程访问
只需要修改data⽬录下的f和f这两个⽂件:
f:配置对数据库的访问权限;
11.1修改f
vi /opt/postgres/9.4/data/f
在IPV4中添加下⾯那⼀⾏内容
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
#表⽰允许任意⽹段的任意机器通过密码验证的⽅式访问到该数据库
重新加载postgresql配置⽂件(可选):
su - postgres
pg_ctl reload
11.2修改f
vi /opt/postgres/9.4/f
定位到listen_addresses,并将localhost改为*
listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart)
max_connections = 1000 # (change requires restart)
注:修改完配置后需要重新启动postgresql远程连接才能⽣效⾄此,postgresql的安装和配置已经全部完成

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。