1

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)
1
  • Can the operator user create schemas in the postgres database? Commented Jul 28, 2017 at 20:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi ecarlin! Thank you for helping. I had it in there but it's still not working :/ But when I do a select statement, it's printing everything ok. Not sure why
You need to commit after executing the DDL (create schema) statement.
Thanks again! I took away (db1) and now its working now. and yes cco, i added that commit. I thought the autocommit would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.