在 Postgres 中创建模式的 Python 未显示

标签 python postgresql

我有这个 super 简单的 Python 代码来连接到 Postgres 并创建一个模式。它没有显示错误,但我没有看到在 Postgres 中添加了架构。我一定错过了一些显而易见的东西。

我什至不确定这是否连接到 Postgres,因为当我给它一个错误的密码时,它仍然没有错误地返回。但是,当我反复试验时,它一直说连接已到位。请帮忙~谢谢!

import psycopg2

psycopg2.autocommit = True

def build_archive(db1):

    db1 = psycopg2.connect(database='postgres', user='operator', password='1234', host='localhost', port='2280')
    cursor = db1.cursor()
    sql = """CREATE SCHEMA app_tools AUTHORIZATION operator;"""
    cursor.execute(sql)

最佳答案

在您的代码中没有实际调用 build_archive() 的地方,因此永远不会执行其中的代码。您需要在函数定义下方添加对 build_archive() 的调用,然后您将开始看到输出。

例如:

import psycopg2

psycopg2.autocommit = True

def build_archive(db1):
    db1 = psycopg2.connect(database='postgres', user='operator',         
           password='1234', host='localhost', port='2280')
    cursor = db1.cursor()
    sql = """CREATE SCHEMA app_tools AUTHORIZATION operator;"""
    cursor.execute(sql)

build_archive()  # Add this call to build_archive()

要了解有关如何在 Python 中使用函数的更多信息,请阅读 here .

关于在 Postgres 中创建模式的 Python 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381870/

相关文章:

python - 可重现的测试框架

python - Django 多个搜索词

sql - 使用默认值重载 Postgres 函数 - 我的函数是不可调用的吗?

postgresql - 在orm.xml(Spring Data/JPA 2.1/Hibernate 5.3.7/Postgresql)中定义@Type?

javascript - 使用 Python 抓取网页的 JavaScript 页面

python - 属性错误 : module 'numpy' has no attribute '__version__'

python - 错误 : etheruem-serpent; version conflict

postgresql - 使用已安装的 PostgreSQL 在 cpanel 中安装 Postgis?

database - 海量 postgres 表的最佳实践

ruby-on-rails - 看不懂的 Rails 3.0 日志