Reputation: 11
import psycopg2
from config import host, user, password, db_name, port
connection = None
try:
# подключаемся к базе
connection = psycopg2.connect(
host=host,
user=user,
password=password,
database=db_name,
port=port
)
connection.autocommit = True
# cursor нужен чтобы взаимодействовать с БД
# сейчас создаем нашу БД
with connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS data1 (
price int NOT NULL,
styles text NOT NULL,
runes text);"""
)
except Exception as _ex:
print("[INFO] Error while working with PostgreSQL", _ex)
finally:
if connection:
connection.close()
print("[INFO] PostgreSQL connection closed")
config
host = "127.0.0.1"
user = "postgres"
password = "14101999"
db_name = "data"
port = "5432"
When executing , it outputs [INFO] Error while working with PostgreSQL
I do not understand at all what the problem is and why the database is not being created, I seem to have indicated everything correctly in config...
Upvotes: 1
Views: 133
Reputation: 19590
I don't see where you are creating a database. psycopg2.connect()
has to connect to an existing database. So either you create the database outside the Python script or you connect to an existing database, best practices the database named postgres, and then issue a CREATE DATABASE <some_name>
. You would then need to close the existing connection and create a new one to the new database.
Upvotes: 1