questiongirl
questiongirl

Reputation: 43

Python to Create Schema in Postgres Not showing up

I have this super simple Python code to connect to Postgres and to create a schema. It shows no error but I don't see the schema being added in Postgres. I must be missing something that's apparent.

I am not even sure if this's connected to Postgres because when I gave it a wrong password, it still returned without errors. But when it keeps saying connection is in place when I do trial and error. Please help~ thanks!

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)

Upvotes: 1

Views: 2100

Answers (1)

ecarlin
ecarlin

Reputation: 1233

No where in your code do you actually call build_archive() so the code inside of it is never executed. You need to add a call to build_archive() below the function definition and then you will start seeing output.

For example:

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()

To learn more about how to use functions in Python read here.

Upvotes: 1

Related Questions