Centos之postgres 安装-yellowcong
  Hlmk1dDVrQDy 2023年11月02日 56 0


本文简单讲解了postgre的安装,以及如何设置远程访问,以及用户及表的创建和postgres的简单操作。

1. 安装postgre

1.1 下载安装

yum install http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-redhat95-9.5-2.noarch.rpm -y

#安装postgresql服务
 yum install postgresql95-server postgresql95-contrib -y

1.2 设定目录

#创建目录
mkdir -p /data/postgresql_data

#设定权限
chown postgres:postgres /data/postgresql_data
chmod 750 /data/postgresql_data

1.3 设定环境变量

export PG_SQL_HOME=/usr/pgsql-9.5
export LD_LIBRARY_PATH=/usr/pgsql-9.5/lib
export PGDATA=/data/postgresql_data

export PATH=$PATH:$PG_SQL_HOME/bin

Centos之postgres 安装-yellowcong_数据库

1.4 初始化数据库

#切换到 postgres 用户
su - postgres

export PGDATA=/data/postgresql_data
#初始化数据库
initdb

# 启动postgres数据库
pg_ctl start -D $PGDATA

先设定postgresql的数据存储目录,先暴漏一个环境变量,然后设定服务。

Centos之postgres 安装-yellowcong_其他_02

启动数据库

1.5 配置service

vi /usr/lib/systemd/system/postgresql-9.5.service

#添加存储的地址
Environment=PGDATA=/data/postgresql_data

Centos之postgres 安装-yellowcong_postgresql_03

1.6 重启服务

#干掉之前启动的实例,不然就会导致服务重复启动不起来
kill -9 $(ps -ef|grep post |awk '{print $2}')


#设置开机启动
systemctl enable postgresql-9.5.service

#启动服务
service postgresql-9.5 start

#查看服务状态
service postgresql-9.5 status

Centos之postgres 安装-yellowcong_sql_04


Centos之postgres 安装-yellowcong_数据库_05

2 开启远程访问

Centos之postgres 安装-yellowcong_postgresql_06

2.1 配置postgresql.conf

查看postgresql的数据目录,然后配置postgresql.conf 文件

配置远程访问。

#设定监听得地址
listen_addresses = '*'

#设定端口
port = 5432

#设定加密
password_encryption = on

Centos之postgres 安装-yellowcong_postgresql_07

2.2 配置pg_hba.conf

配置设定验证方式,以及设定所有ip都可以链接

host all all 0.0.0.0/0 md5

Centos之postgres 安装-yellowcong_sql_08

重启服务后,配置生效

#重启post服务
service postgresql-9.5 restart

#查看post信息
 ps -ef|grep post
 
 #查看端口号信息
 netstat -nptl

Centos之postgres 安装-yellowcong_sql_09

3 创建数据库

3.1 登陆数据库

然后使用psql工具登录数据库,列出当前的数据库,命令分别是 psql 和 \l

Centos之postgres 安装-yellowcong_postgresql_10

#创建数据库
create database yellowcong;

#创建数据库的用户
CREATE USER huangcong CREATEDB LOGIN PASSWORD 'admin';

#数据库设置授权
GRANT ALL ON DATABASE yellowcong TO huangcong ;

#退出服务
\q

Centos之postgres 安装-yellowcong_数据库_11

3.2 用户远程登陆

psql -U huangcong -h 127.0.0.1 -p 5432 -d yellowcong -W

#切换数据库
\c postgres

#查看当前的表信息
\d

可以看到远程链接成功

Centos之postgres 安装-yellowcong_数据库_12

创建了一个简单的表,然后通过\d来查看当前的表信息。

Centos之postgres 安装-yellowcong_数据库_13

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   28天前   19   0   0 数据库
Hlmk1dDVrQDy