[学习笔记]PostgreSQL数据库的安装和配置

安装

安装源

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

安装本体

yum -y install postgresql12 postgresql12-server postgresql12-contrib

初始化数据库

/usr/pgsql-12/bin/postgresql-12-setup initdb

配置PostgreSQL服务自启动,并开启服务

 systemctl enable postgresql.service
 systemctl start postgresql.service

配置

配置默认用户的密码

su - postgres
psql
alter user postgres with password '[新密码]';

注意最后有个分号;不要漏了!

配置远程访问

cd /var/lib/pgsql/12/data

先备份原始配置

mv pg_hba.conf pg_hba.conf_bak

在该目录下创建新的配置文件pg_hba.conf

local   all             all                                     md5
host    all             all             0.0.0.0/0               md5
host    replication     replica         0.0.0.0/0               md5

配置postgresql.conf

sed -i "s#\#listen_addresses.*#listen_addresses='*'#g" /var/lib/pgsql/12/data/postgresql.conf
sed -i  's#max_connections = 100#max_connections = 500#g' /var/lib/pgsql/12/data/postgresql.conf

重启服务

systemctl restart postgresql-12.service

再用默认用户试试,此时会要求输入密码,输入正确后进入psql命令模式

在这里插入图片描述
配置防火墙

firewall-cmd --zone=public --add-port=5432/tcp --permanent

使用数据库管理工具远程连接测试
在这里插入图片描述
至此完成所有的安装和配置工作

PostgreSQL的安装详细信息请参考官网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在JavaWeb开发中,我们通常使用数据库来存储和管理数据。而PostgreSQL是一种常见的开源关系型数据库,它支持ACID事务,具有高可靠性和安全性。 在使用PostgreSQL时,我们通常使用JDBC驱动程序来连接数据库。而连接池可以帮助我们更有效地管理数据库连接,提高应用程序的性能。 在Servlet中配置PGPoolingDataSource,可以通过以下步骤完成: 1. 添加PostgreSQL JDBC驱动程序 在项目中添加PostgreSQL JDBC驱动程序的jar包。可以从PostgreSQL官网下载最新版本的JDBC驱动程序。将其添加到项目的classpath中。 2. 配置PGPoolingDataSource 在Servlet的init()方法中,创建PGPoolingDataSource对象。PGPoolingDataSource是PostgreSQL JDBC驱动程序提供的连接池类,可以通过设置其属性来配置连接池。常用的属性有: - serverName:数据库服务器名称 - portNumber:数据库服务器端口号 - databaseName:数据库名称 - user:数据库用户名 - password:数据库密码 - maxConnections:连接池中允许的最大连接数 在配置属性后,调用PGPoolingDataSource的setDataSourceName()方法设置数据源名称,调用PGPoolingDataSource的setMaxConnections()方法设置连接池中最大的连接数。 示例代码如下: ``` import java.sql.*; import org.postgresql.jdbc2.*; public class MyServlet extends HttpServlet { private PGPoolingDataSource dataSource; public void init() throws ServletException { dataSource = new PGPoolingDataSource(); dataSource.setDataSourceName("myDataSource"); dataSource.setServerName("localhost"); dataSource.setDatabaseName("myDatabase"); dataSource.setUser("myUsername"); dataSource.setPassword("myPassword"); dataSource.setMaxConnections(10); } // ... } ``` 3. 获取数据库连接 在需要访问数据库时,可以通过dataSource.getConnection()方法获取一个数据库连接。使用完后,需要调用Connection.close()方法释放连接。 示例代码如下: ``` import java.sql.*; import org.postgresql.jdbc2.*; public class MyServlet extends HttpServlet { private PGPoolingDataSource dataSource; public void init() throws ServletException { // ... } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; try { connection = dataSource.getConnection(); // use connection } catch (SQLException e) { // handle exception } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { // handle exception } } } } } ``` 以上就是在Servlet中配置PGPoolingDataSource的步骤。通过连接池,可以更好地管理数据库连接,提高应用程序的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林晓lx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值