CentOS7 下安装 PostgreSQL10


 
方法一:

1、Install therepository RPM:

yum installhttps://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat10-10-2.noarch.rpm

2、Install the clientpackages:

yum install postgresql10

3、Optionally installthe server packages:

yum install postgresql10-server

4、Optionallyinitialize the database and enable automatic start:

/usr/pgsql-10/bin/postgresql-10-setupinitdb

systemctl enable postgresql-10

systemctl start postgresql-10

5、FOR RHEL / CENTOS /SL / OL 5,6

 service postgresql initdb

 chkconfig postgresql on

6、FOR RHEL / CENTOS /SL / OL 7 OR FEDORA 24 AND LATER DERIVED DISTRIBUTIONS:

 postgresql-setup initdb

 systemctl enable postgresql.service

 systemctl start postgresql.service

配置使用
1. 修改用户密码
#yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL;
[root@psql_master ~]# su - postgres
#切换用户后,提示符变更为“postgres@pg-1:/var/lib/pgsql#”;
#同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成;
#PostgreSQL在数据库用户同名的系统账号下登录免密;

postgres@pg-1:/var/lib/pgsql# psql -U postgres

#进入数据库后修改密码;
postgres=# alter user postgres with password 'postgres@123'

root@pg-1:/root# systemctl start postgresql-10
root@pg-1:/root# su - postgres
postgres@pg-1:/var/lib/pgsql# psql -U postgres
psql (10)
Type "help" for help.
postgres=# alter user postgres with password 'm2018'
postgres-# 

2. 允许远程访问
#配置文件中,默认只能本机访问postgresql;
#修改listen_addresses = 'localhost'为listen_addresses = '*',允许所有远程访问;
#修改配置文件需要重启服务。
[root@psql_master ~]# sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" /opt/postgresql-10/10/data/d
或者
在安装目录下
/opt/postgresql-10/10/data/data/pg_hba.conf添加以下内容
listen_addresses = '*'

3. 主机认证
#在第82行之后,”IPv4 local connections”下新增允许的客户端;
#“host” 代表主机类型,第一个“all”代表db ,第二个“all”代表user ,“172.29.3.67/32” 代表client ip,“trust”代表认证方式;
#认证方式除“trust”外,还有“peer”, “ident”, “md5”, “password”等,具体可参考pg-hba文件: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html
#修改pg.hba文件需要重启服务。
[root@psql_master ~]# vim /opt/postgresql-10/10/data/data/pg_hba.conf
host    all             all             172.29.3.67/32          trust

4. 设置环境变量
[root@psql_master ~]# vim /etc/profile
export PATH=$PATH:/usr/pgsql-10/bin
[root@psql_master ~]# source /etc/profile

5. 重启服务
[root@psql_master ~]# systemctl restart postgresql-10

6. iptables
#postgresql默认开启tcp5432端口
[root@psql_master ~]# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

[root@psql_master ~]# service iptables restart

四.使用验证
1. 查看端口
[root@psql_master ~]# netstat -tunlp

2. 简单使用
1)创建用户
postgres=# create user postuser1 with password 'user1@123';

2)创建数据库
#同时指定数据库的所有者
postgres=# create database postdb1 owner postuser1;

3)数据库赋权
#未赋权则账户只能登录控制台
postgres=# grant all privileges on database postdb1 to postuser1;

4)登录新建数据库
#在操作系统层使用新建的账号登录新建的数据库,登录后提示符为“postdb1=>”;
#如果在postgres账户下直接使用“postgres=# \c postdb1;”登录,则登录用户依然是postgres,
-bash-4.2$ psql -U postuser1 -d postdb1 -h 127.0.0.1 -p 5432

5)创建表
postdb1=> create table tb1(
          id int primary key,
          name VARCHAR(20), 
          salary real
          );


6)插入数据
postdb1=> insert into tb1(
          id, name, salary)
          values(
          101, 'Mike', 5000.00 );

7)查询
postdb1=>select * from tb1;

方法二:

1、下载源代码并解压

https://www.postgresql.org/download/linux/redhat/

wgethttps://ftp.postgresql.org/pub/source/v9.4.15/postgresql-9.4.15.tar.gz

tar -xvzf postgresql-10.0.tar.gz   

./configure

sudo make

sudo make install

2、创建用户组和用户

groupadd postgres 

useradd -g postgres postgres  

 passwd postgres 

3、创建数据目录

 mkdir /usr/local/pgsql/data

chown postgres /usr/local/pgsql/data  

 chmod700 /usr/local/pgsql/data   

4、数据库操作

 /usr/local/pgsql/bin/initdb -D/usr/local/pgsql/data    #初始化数据库

/pg_ctl start\stop\restart -D/usr/local/pgsql/data/   #启动\停止\重启数据库

/usr/local/pgsql/bin/postgres -D/usr/local/pgsql/data >logfile 2>&1 &  #设置日志输出位置

/usr/local/pgsql/bin/createdb test    #创建测试数据库

/usr/local/pgsql/bin/psql test    #启动测试数据库

5、修改postgresql.conf

 listen_addresses = '*'

 port= 5432

6、修改pg_hba.conf

# "local" is for Unix domainsocket connections only

local  all             all                                  trust

# IPv4 local connections:

host   all             all             0.0.0.0/0            trust

7、远程连接

1)        查看防火墙是否关闭:firewall-cmd --state

2)        启动服务:systemctl start firewalld.service

3)        关闭服务:systemctl stop firewalld.service

4)        重启服务:systemctl restart firewalld.service

5)        显示服务的状态:systemctl status firewalld.service

6)        在开机时启用服务:systemctl enable firewalld.service

7)        在开机时禁用服务:systemctl disable firewalld.service

8)        查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?

9)        查看已启动的服务列表:systemctl list-unit-files|grep enabled

10)    添加开发端口:firewall-cmd --zone=public --add-port=5432/tcp --permanent

11)    重新加载防火墙:firewall-cmd --reload

发布了17 篇原创文章 · 获赞 2 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/u011250186/article/details/103770656