在Python中连接PostgreSQL(通常简称为PG)数据库,你可以使用`psycopg2`库,这是Python中最常用的PostgreSQL数据库适配器。以下是如何使用`psycopg2`连接到PostgreSQL数据库的步骤:
1. 安装`psycopg2`库。虽然你要求不使用`pip`安装,但在实际操作中,你需要首先安装这个库。以下是安装`psycopg2`的命令:
```bash
pip install psycopg2
```
或者,如果你不想安装完整的`psycopg2`库,可以使用它的纯Python版本`psycopg2-binary`:
```bash
pip install psycopg2-binary
```
2. 使用`psycopg2`模块连接到PostgreSQL数据库。以下是一个简单的示例代码:
```python
import psycopg2
数据库连接参数
db_params = {
'dbname': 'your_database_name', 数据库名
'user': 'your_username', 用户名
'password': 'your_password', 密码
'host': 'your_host', 服务器地址,默认是localhost
'port': 'your_port', 服务器端口号,默认是5432