×

PostgreSQL 入门-安装篇_postgresql安装

前端技术网 前端技术网 发表于2024-01-18 16:47:03 浏览2035 评论0

抢沙发发表评论

一、虚拟机中如何安装PostgreSQL数据库

配置PostgreSQL对应的yum源

使用命令:dnf install-y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm,获取yum配置文件。

PostgreSQL 入门-安装篇_postgresql安装

安装PostgreSQL13对应的rpm包

一般来说PostgreSQL需要安装的包包括:

依赖库包:postgresql13-libs.x86_64

模块&分布式:postgresql13-contrib.x86_64

客户端:postgresql13.x86_64

服务端:postgresql13-server.x86_64

PostgreSQL 入门-安装篇_postgresql安装
创建postgres用户及组

groupadd postgres创建postgres组

useradd-g postgres创建postgres用户

初始化数据库

安装postgresql数据库后,默认的数据目录为:/var/lib/pgsql/10/data/。但是在实际使用中,我们必须为postgresql数据库指定专用的数据路径。

1、创建指定路径

mkdir-p/data/pgdata

2、修改pgdata的所有者

chown postgres/data/pgdata

3、切换到postgres用户

su- postgres?

4、初始化postgres数据库实例

/usr/pgsql-13/bin/initdb-D/data/pgdata

到这里就安装完成了。

二、如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin

PostgreSQL是一个强大开源的对象关系类型数据库系统,它能运行于几乎所有主要的操作系统,包括Linux、Unix(AIX、BSD、HP-UX、SGI IRIX、Mac OS、Solaris、Tru64)、Windows OS。在这篇教程里,我们将学习如何在CentOS7/6.5/6.4 server中建立PostgreSQL。

1.安装PostgreSQL

首先根据你的服务器架构添加PostgreSQL库:

对CentOS 6.x 32bit:

rpm-Uvh http://yum.postgresql.org/9.3/RedHat/rhel-6-i386/pgdg-centos93-9.3- 1.noarch.rpm

对CentOS 6.x 64bit:

rpm-Uvh http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm

对CentOS 7 64bit:

rpm-Uvh http://yum.postgresql.org/9.3/redhat/rhel-7-x86_64/pgdg-centos93-9.3-1.noarch.rpm

对于其它的发行版,可查看以下链接并建立库:

http://yum.postgresql.org/repopackages.php#pg93

使用以下命令来更新库:

yum update

使用以下命令来安装PostgreSQL:

yum install postgresql93-server postgresql93-contrib

使用以下命令来初始化PostgreSQL数据库:

在CentOS 6.x系统中:

service postgresql-9.3 initdb

在CentOS 7系统中:

/usr/pgsql-9.3/bin/postgresql93-setup initdb

然后启动PostgreSQL服务并使之开机自启:

在CentOS 6.x系统中:

service postgresql-9.3 start

chkconfig postgresql-9.3 on

在CentOS 7系统中:

systemctl enable postgresql-9.3

systemctl start postgresql-9.3

2.调整Iptables/Firewall

接下来调整防火墙出站规则:

在CentOS 6.x系统中:

vi/etc/sysconfig/iptables

并添加以下行

-A INPUT-m state--state NEW-m tcp-p tcp--dport 5432-j ACCEPT

-A INPUT-m state--state NEW-m tcp-p tcp--dport 80-j ACCEPT

退出并保存文件。重启iptables服务:

service iptables restart

在CentOS系统中:

firewall-cmd--permanent–add-port=5432/tcp

firewall-cmd--permanent–add-port=80/tcp

firewall-cmd--reload

3.访问PostgreSQL常用的命令提示符

默认情况下数据库名和用户名都是postgres。切换至用户以执行相关操作:

su– postgres

输入以下命令登陆:

psql

会有以下样例输出:

psql(9.3.5)

Type"help" for help.

Postgres=#

可通过输入\q退出postgresql返回命令终端:

4.设置用户密码

登陆至postgres命令提示符界面

su– postgres

psql

然后使用命令设置密码

postgres=#\password postgres

Enter new password:

Enter it again:

postgres=#\q

输入命令以建立PostgreSQL系统管理工具

postgres=# CREATE EXTENSION adminpack;

CREATE EXTENSION

5.创建用户和数据库

例如:用户名:senthil密码:centos数据库名:mydb

转到postgres用户

su– postgres

创建用户senthil

$ createuser senthil

创建数据库

$ createdb mydb

现在登陆至psql提示符界面,为用户senthil设置密码及授权对数据库mydb的访问:

$ psql

psql(9.3.5)

Type"help" for help.

postgres=# alter user senthil with encrypted password'centos';

ALTER ROLE

postgres=# grant all privileges on database mydb to senthil;

GRANT

postgres=#

6.删除用户和数据库

首先转到postgres界面

su– postgres

输入命令

$ dropdb<database-name>

删除用户名可输入

$ dropuser<user-name>

7.配置PostgreSQL-MD5认证

MD5认证需要客户端提供一个MD5-encrypted密码以便身份验证。你需要编辑/var/lib/pgsql/9.3/data/pg_hba.conf文件:

vi/var/lib/pgsql/9.3/data/pg_hba.conf

添加或修改的行如下:

[...]

# TYPE DATABASE USER ADDRESS METHOD

#"local" is for Unix domain socket connections only

local all all md5

# IPv4 local connections:

host all all 127.0.0.1/32 md5

host all all 192.168.1.0/24 md5

# IPv6 local connections:

host all all::1/128 md5

[...]

重启postgresql服务以应用更改

在CentOS 6.x系统中

service postgresql-9.3 restart

在CentOS 7系统中

systemctl restart postgresql-9.3

8.配置PostgreSQL-Configure TCP/IP

默认情况下,TCP/IP连接是不可行的,所以其他计算机用户不能连接到postgresql。编辑文件/var/lib/pgsql/9.3/data/postgresql.conf可以允许连接:

vi/var/lib/pgsql/9.3/data/postgresql.conf

找到下面的行:

[...]

#listen_addresses='localhost’

[...]

#port= 5432

[...]

把两行都取消并设置为你的postgresql服务器IP地址或设置为“*”监听所有客户端。如下所示:

listen_addresses='*'

port= 5432

重启以应用更改

在CentOS6.x系统中:

/etc/init.d/postgresql-9.3 restart

在CentOS7系统中:

systemctl restart postgresql-9.3

9.使用phpPgAdmin管理PostgreSQL

phpPgAdmin是使用PHP编写的基于web的管理工具,用于管理PostgreSQL。它只适用与PostgreSQL RPM库。

如果你没有添加PostgreSQL库,你可以添加EPEL库。

可根据下面的链接在CentOS 6.x中建立EPEL库

http://www.unixmen.com/install-epel-repository-rhel-centos-scientific-linux-6/

CentOS 7的话参考下面的链接

http://www.unixmen.com/install-epel-repository-centos-rhel-7/

使用命令更新库

yum update

现在输入命令安装phpPgAdmin:

yum install phpPgAdmin httpd

注意phpPgAdmin区分大小写,要准确使用上面所示的大小写!

默认你可以使用http://localhost/phpPgAdmin访问phpPgAdmin。若要远程访问需要继续:

编辑文件/etc/httpd/conf.d/phpPgAdmin.conf

vi/etc/httpd/conf.d/phpPgAdmin.conf

修改如下加粗的部分:

[...]

Alias/phpPgAdmin/usr/share/phpPgAdmin

<Location/phpPgAdmin>

<IfModule mod_authz_core.c>

# Apache 2.4

Require all granted

#Require host example.com

</IfModule>

<IfModule!mod_authz_core.c>

# Apache 2.2

Order deny,allow

Allow from all

# Allow from.example.com

</IfModule>

</Location>

启动或重启Apache服务

在CentOS 6.x系统中

service httpd start

chkconfig httpd on

在CentOS 7系统中

systemctl enable httpd

systemctl start httpd

现在打开浏览器并转到http://ip-address/phpPgAdmin。终于看到下面的界面了!

使用你之前创建的用户登录,我的是用户senthil密码CentOS。

你可能会遇到:Login failed。

这是因为SELLinux可能限制用户连接到PostgreSQL,只需输入以下命令更改即可:

setsebool-P httpd_can_network_connect_db 1

现在你应该能正常登录了。

三、如何安装PostgreSQL

右击文件选择以管理员身份运行

开始执行程序的安装

设置安装目录

设置数据的保存目录

设置数据库管理员密码,请牢记此密码。

设置端口号,选择默认的端口号即可

根据自己选择设置地区:这里选择中国

正式安装

9

完成安装

END

三、PostgreSQL的启动确认

1

打开控制面板-找到管理工具-服务

2

检查服务对话框PostgreSQL的状态,若为开始,则表示它已经正常启动了。

END

经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。

举报作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。

投票(2)

有得(0)

我有疑问(0)

换一批相关经验

kali2.0下配置Metasploit+postgresql链接82016.02.19

postgresql安装(linux下)12015.10.26

postgresql数据库在linux(centos)上的安装22014.11.30

IIS的安装与配置详细图解教程。702013.05.08

超详细图解Java8下载安装,Java1.8环境变量配置312015.02.26

关于PostgreSQL 入门-安装篇_postgresql安装和如何安装PostgreSQL的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。