0

For some unknown reason table in database was not created. How to solve it?

import psycopg2
conn = psycopg2.connect(user='postgres', host='localhost', password='123456')
conn.autocommit = True
cur = conn.cursor()
cur.execute('CREATE DATABASE example;')
cur.execute("""CREATE TABLE HARMKA
                         (ID INT PRIMARY KEY NOT NULL,
                         PHARMACY_LICENSE CHAR(100),
                         STREET CHAR(150),
                         CITY CHAR(30));""")
cur.execute("INSERT INTO HARMKA VALUES(%s, %s, %s, %s)", (1, '12345', 'street', 'Denwer'))
cur.close()
conn.close()
4
  • If you'd tell us what went wrong, what error you received or what exactly you mean by "failed".. I mean that would be just nifty... Commented Jan 25, 2018 at 7:38
  • There is no error. Database is created, but table is not in Database. Commented Jan 25, 2018 at 7:39
  • What if you also commit the transactions? conn.commit() Commented Jan 25, 2018 at 7:42
  • 1
    Adelin I think conn.autocommit = True is the same Commented Jan 25, 2018 at 8:14

1 Answer 1

3

You need to connect to example database and then create tables and manipulate data.

conn = psycopg2.connect(database='example', user='postgres', host='localhost', password='123456')
Sign up to request clarification or add additional context in comments.

2 Comments

import psycopg2 conn = psycopg2.connect(user='postgres', host='localhost', password='123456') conn.autocommit = True cur = conn.cursor() cur.execute('CREATE DATABASE example;') conn = psycopg2.connect(database = 'example', user='postgres', host='localhost', password='123456') conn.autocommit = True cur = conn.cursor()
@FootAdministration glad it helped!

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.