ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

安装postgresql11.5

2019-11-10 15:02:18  阅读:366  来源: 互联网

标签:postgres root pgsql postgresql11.5 localhost 安装 dir build


 

root身份安装

创建用户

编译安装成功后,接下来要做的就是创建一个普通用户,因为默认超级用户(root)不能启动postgresql,所以需要创建一个普通用户来启动数据库,执行以下命令创建用户:

[root@localhost build_dir]# groupadd postgres
[root@localhost build_dir]# useradd -g postgres postgres
[root@localhost build_dir]# passwd postgres

接下来设置权限,将pg的数据目录全部赋给postgres用户,执行以下命令:

[root@localhost build_dir]# chown -R postgres:postgres /usr/local/pgsql

创建目录

[root@localhost build_dir]# mkdir -p /mnt/db1/pgdata/pgsql /mnt/db1/pgdata/pgtbs /mnt/db1/archivelog /backups
[root@localhost build_dir]# chmod -R 775 /mnt/db1
[root@localhost build_dir]# chown -R postgres:postgres /mnt/db1

设置环境变量

[root@localhost build_dir]# vi /home/postgres/.bash_profile

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

export PGPORT=8432
export PGHOME=/usr/local/pgsql
export PGDATA=/mnt/db1/pgdata/pgsql
export PATH=$PGHOME/bin:$PATH
export MANPATH=$PGHOME/share/man:$MANPATH
export LANG=en_US.UTF-8
export DATE='date +"%Y%m%d%H%M"'
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH

执行如下命令使其生效:

[root@localhost build_dir]# source /home/postgres/.bash_profile

 

 

 

 

 

 

准备

yum install -y 
gcc gcc-c++  
openssl openssl-devel
readline readline-devel 
zlib zlib-devel 
llvm5.0 llvm5.0-devel 
libxml2-devel 
libxslt-devel 
libicu-devel 

python-devel
tcl-devel 
systemd-devel 
openldap-devel 
pam-devel 

clang 
perl-ExtUtils-Embed 
epel-release 


 

 

configure

# cd build_dir/
vim ../src/Makefile.global.in
修改以下行:
COMPILE.c.bc = $(CLANG) -Wno-ignored-attributes $(BITCODE_CFLAGS) $(CPPFLAGS) -flto=thin -emit-llvm -c
修改为:
COMPILE.c.bc = $(CLANG) -Wno-ignored-attributes $(BITCODE_CFLAGS) $(CPPFLAGS) -emit-llvm -c

-- --prefix 指定默认安装路径
[root@localhost build_dir]# ../configure 
--prefix=/usr/local/pgsql 
--enable-nls 
--with-perl 
--with-python 
--with-tcl 
--with-gssapi 
--with-llvm LLVM_CONFIG='/usr/lib64/llvm5.0/bin/llvm-config' 
--with-icu 
--with-openssl 
--with-pam 
--with-ldap 
--with-systemd 
--with-libxml 
--with-libxslt


#configure 命令完成后,会发现创建了 config.status 配置文件

 

 

make
make install

 

 

 

 

初始化数据库

切换用户
[root@localhost build_dir]# su - postgres
初始化数据库
[postgres@localhost ~]$ initdb -D $PGDATA -U postgres --locale=en_US.UTF8 -E UTF8

    修改监听地址 将listen_addresses的值设置成*,使其监听整个网络,端口号默认是5432,也可以自己设置。

[postgres@localhost ~]$ vim /mnt/db1/pgdata/pgsql/postgresql.conf
修改内容:
listen_addresses = '*'
unix_socket_directories = '.'
port = 8432

    修改客户端认证方式

[postgres@localhost ~]$ vim /mnt/db1/pgdata/pgsql/pg_hba.conf
添加内容:
host all all 0.0.0.0/0 md5 # 其他用户登陆

设置防火墙规则

#切换回root用户
[postgres@localhost ~]$ exit
[root@localhost build_dir]# firewall-cmd --zone=public --add-port=8432/tcp --permanent
[root@localhost build_dir]# firewall-cmd --reload

启动数据库

[root@localhost build_dir]# su - postgres
启动
[postgres@localhost ~]$ pg_ctl -D /mnt/db1/pgdata/pgsql -l /mnt/db1/archivelog/pgsql.log start
停止
[root@localhost postgres]# pg_ctl -D /mnt/db1/pgdata/pgsql/ -s -m fast stop

连接测试

[postgres@localhost ~]$ psql
查询所有用户
postgres=# select * from pg_user;
postgres=# select * from pg_roles;
查询权限
postgres=# select * from information_schema.table_privileges where grantee='cc';
查看有哪些数据库
postgres=# \l
相当与mysql的show databases;
postgres=# select datname from pg_database;
相当于mysql的show tables, public 是默认的schema的名字
postgres=# SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
相当与mysql的describe table_name, 'table_name'是要查询的表的名字
postgres=# SELECT column_name FROM information_schema.columns WHERE table_name ='table_name';
退出
postgres=# \q

psql 是 PostgreSQL 的客户端程序,要连接 PostgreSQL 数据库,我们需要指定以下内容:

    -d or --dbname 数据库名
    -h or --host 主机名
    -p or --port 端口号,默认5432 端口
    -U or --username 用户名
    [postgres@localhost ~]$ psql -h localhost -p 8432 -U postgres

设置postgres用户密码

[postgres@localhost ~]$ psql
postgres=# ALTER USER postgres WITH encrypted PASSWORD 'new password';
postgres=# \q
[postgres@localhost ~]$ psql -h localhost -p 8432 -U postgres

设置开机自启动

    设置启动配置

vim /usr/lib/systemd/system/postgresql-11.service
添加内容:
[Unit]
Description=PostgreSQL 11 database server
Documentation=https://www.postgresql.org/docs/11/static/
After=syslog.target
After=network.target

[Service]
Type=notify

User=postgres
Group=postgres

# Location of database directory
Environment=PGDATA=/mnt/db1/pgdata/pgsql/

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000

Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0

# ExecStartPre=/usr/pgsql-11/bin/postgresql-11-check-db-dir ${PGDATA}
ExecStart=/usr/local/pgsql/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
# ExecStart=/usr/local/pgsql9.4/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
# ExecStop=/usr/local/pgsql9.4/bin/pg_ctl stop -D ${PGDATA} -s -m fast
# ExecReload=/usr/local/pgsql9.4/bin/pg_ctl reload -D ${PGDATA} -s
KillMode=mixed
KillSignal=SIGINT

# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0

[Install]
WantedBy=multi-user.target

    添加可执行权限

[root@localhost postgres]# chmod 754 /usr/lib/systemd/system/postgresql-11.service

    设置开机自启动

自动启动
[root@localhost postgres]# systemctl enable postgresql-11.service
启动
[root@localhost postgres]# systemctl start postgresql-11.service
停止某服务
[root@localhost postgres]# systemctl stop postgresql-11.service
不自动启动
[root@localhost postgres]# systemctl disable postgresql-11.service
检查服务状态(服务详细信息)
systemctl status postgresql-11.service
检查服务状态(仅显示是否Active)
systemctl is-active postgresql-11.service
显示所有已启动的服务
systemctl list-units --type=service

 

 

 

 

 

 

标签:postgres,root,pgsql,postgresql11.5,localhost,安装,dir,build
来源: https://www.cnblogs.com/igoodful/p/11830219.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有