Postgres Psycopg2 创建 Table

Postgres Psycopg2 Create Table

我是 Postgres 的新手并且 Python。我正在尝试创建一个简单的用户 table,但我不知道为什么它没有创建。 错误消息没有出现,

    #!/usr/bin/python
    import psycopg2

    try:
        conn = psycopg2.connect(database = "projetofinal", user = "postgres", password = "admin", host = "localhost", port = "5432")
    except:
        print("I am unable to connect to the database") 

    cur = conn.cursor()
    try:
        cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    except:
        print("I can't drop our test database!")

    conn.close()
    cur.close()

您忘记提交数据库!

import psycopg2

try:
    conn = psycopg2.connect(database = "projetofinal", user = "postgres", password = "admin", host = "localhost", port = "5432")
except:
    print("I am unable to connect to the database") 

cur = conn.cursor()
try:
    cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
except:
    print("I can't drop our test database!")

conn.commit() # <--- makes sure the change is shown in the database
conn.close()
cur.close()

`