docker 启动 postgres 15 设置密码为:zR6c49Mmmf4Rc6Vk请帮我写出docker-compose 配置。并使用python 测试连接 postgres 数据库
version: '3.1' services: db: image: postgres:15 restart: always environment: POSTGRES_PASSWORD: zR6c49Mmmf4Rc6Vk ports: - "5432:5432" volumes: - db-data:/var/lib/postgresql/data volumes: db-data:
Python 连接 Postgres 数据库示例代码:
import psycopg2
conn = psycopg2.connect(
host="localhost",
database="postgres",
user="postgres",
password="zR6c49Mmmf4Rc6Vk"
)
cur = conn.cursor()
cur.execute("SELECT version();")
print(cur.fetchone())
cur.close()
conn.close()
原文地址: https://www.cveoy.top/t/topic/bSyF 著作权归作者所有。请勿转载和采集!