python - 与 Postgres 数据库通信时如何保护 Pandas 方法?

标签 python postgresql pandas sqlalchemy

Python Pandas 有一个 to_sqlread_sql可用于与数据库通信的方法。

我正在连接到 Postgres 数据库。 Pandas 似乎使用 SQL Alchemy and psycopg2执行通信。

当使用 create_engine('mysql+mysqlconnector://user:password@amazon_endpoint:port/database_name',connect_args={'sslmode':'require'}, echo=True) 我收到了AttributeError: Unsupported argument 'sslmode' 错误。

然后我看到了以下帖子:1 , 2并将代码修改为 create_engine('mysql+mysqlconnector://user:password@amazon_endpoint:port/database_name?ssl_ca=/home/user/Downloads/rds-combined-ca-bundle.pem')。现在连接正常,我可以连接到数据库了。这是否意味着连接现在是安全的?有什么方法可以确认这一点吗?

最佳答案

我不认为默认情况下连接是安全的,除非您的 Postgres 实例已配置为需要安全连接。首先,您的实例需要使用 SSL 支持构建,然后告诉客户端安全连接是一件简单的事情。来自 the documentation :

PostgreSQL has native support for using SSL connections to encrypt client/server communications for increased security. This requires that OpenSSL is installed on both client and server systems and that support in PostgreSQL is enabled at build time (see Chapter 15).

With SSL support compiled in, the PostgreSQL server can be started with SSL enabled by setting the parameter ssl to on in postgresql.conf. The server will listen for both normal and SSL connections on the same TCP port, and will negotiate with any connecting client on whether to use SSL. By default, this is at the client's option; see Section 19.1 about how to set up the server to require use of SSL for some or all connections.

当您使用 to_sqlread_sql 时,向其传递一个使用 sslmode=require 设置的 SQLAlchemy 连接。 This question提供了用于设置连接的完整代码段:

db = create_engine(
  'postgresql+pg8000://user:pass@hostname/dbname',
  connect_args={'sslmode':'require'},
  echo=True
).connect()

关于python - 与 Postgres 数据库通信时如何保护 Pandas 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32492864/

相关文章:

python - 如何更改seaborn中因子图的顺序

python - 如何使用 tf.train.Scaffold 初始化评估的局部变量?

SQL:运行总出现次数

SQL删除查询忽略错误并继续

python - 当每个字典都有不同的键时,如何按值对字典列表进行排序?

python - 如何将 lambda 函数应用于多列

python - Raspberry Pi 3 B+ BLE 升级后扫描失败(使用Stretch)

python - 如何在Python中安排周期性任务?

ruby-on-rails - 如何从子模型中总结父模型记录

python - 列表字典到数据框