我正在用Python开发一个CRUD,它将对一个项目很有用。但是,我希望将当前在PostgreSQL中创建的数据库连接到一种联机服务器或联机数据库,以便在信息丢失的情况下,不必重复备份,而是要将所有信息联机或使我的计算机作为服务器工作。我该怎么做呢?
发布于 2022-04-11 14:28:58
好的,首先您需要通过执行、pip安装psycopg2或使用pip3安装带有pip的psycopg2,然后尝试一个示例,如。
from psycopg2 import Error
try:
# Connect to an existing database
connection = psycopg2.connect(user="postgres",
password="pynative@#29",
host="127.0.0.1",
port="5432",
database="postgres_db")
# Create a cursor to perform database operations
cursor = connection.cursor()
# Print PostgreSQL details
print("PostgreSQL server information")
print(connection.get_dsn_parameters(), "\n")
# Executing a SQL query
cursor.execute("SELECT version();")
# Fetch result
record = cursor.fetchone()
print("You are connected to - ", record, "\n")
except (Exception, Error) as error:
print("Error while connecting to PostgreSQL", error)
finally:
if (connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed") 发布于 2022-04-11 04:18:34
您可能需要尝试psycopg2库。PostgreSQL是用于programming语言的一个适配器。它用许多扩展实现了PEP 249。
https://stackoverflow.com/questions/71822617
复制相似问题