PostgreSQL数据库在线

发布于 2025-01-20 13:20:00 字数 127 浏览 1 评论 0原文

我正在Python开发一个CRUD,这对于一个项目很有用。但是,我想将我当前在PostgreSQL创建的数据库连接到一种在线服务器或在线DB,因此,如果信息丢失,则不必进行重复备份,而是要有所有信息在线或使我的计算机用作服务器。我该怎么做?

I am developing a CRUD in Python, which will be useful for a project. However, I want to connect the database that I currently have created in PostgreSQL to a kind of online server or online DB, so that in case there is loss of information, it is not necessary to make a recurring backup, but to have all information online or make my computer work as a server. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撞了怀 2025-01-27 13:20:00

好的,首先您需要通过 pip install 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") 

Okay, first you'll want to the install psycopg2 with pip by doing, pip install psycopg2 or use pip3 then you'll want to try an example such as.

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") 
旧竹 2025-01-27 13:20:00

您可能想尝试 psycopg2 库。 Psycopg 是 Python 编程语言的 PostgreSQL 适配器。它实现了 PEP 249 并进行了许多扩展。

这是一个链接: https://pynative.com/python-postgresql-tutorial/

You might want to try the psycopg2 library. Psycopg is a PostgreSQL adapter for the Python programming language. It implements PEP 249 with many extensions.

Here is a link: https://pynative.com/python-postgresql-tutorial/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文