C
C
che_aa2021-11-02 19:54:57
Python
che_aa, 2021-11-02 19:54:57

How to open all databases in postgresql?

Why doesn't it show new tables created from psycopg2?
Code example (where there are all the basic actions with the table)

example
import psycopg2
import sys
import datetime
from psycopg2 import Error



try:
  db = psycopg2.connect(user="postgres",password="*",host="127.0.0.1",port="5432")
  cursor = db.cursor()
except (Exception, Error) as e:
  print('error: ', e)
  sys.exit()
try:
  cursor.execute('CREATE DATABASE private_db')
except Error :
  print('error: ', Error)
  db.rollback()
cursor.execute('''CREATE TABLE IF NOT EXISTS test_table (
  bool_data BOOL,
  float_data REAL,
  int_data BIGINT,
  str_data TEXT,
  datetime_data TIMESTAMP
)''')
db.commit()

cursor.execute('INSERT INTO test_table VALUES (%s, %s, %s, %s, %s)', (True, 1.1, 1, 'sd', datetime.datetime.now())) 
db.commit()
cursor.execute("SELECT * FROM test_table WHERE bool_data = %s", [True])
result = cursor.fetchall()
print(result)
cursor.execute('UPDATE test_table SET int_data = %s WHERE bool_data = %s', (2, False))
db.commit()
cursor.execute('DELETE FROM test_table WHERE int_data = %s', [1])
db.commit()


cursor.close()
db.close()

The code works fine, no errors. But there is no new table in pgadmin4.
I work with postgres for the first time, if the question is stupid, do not judge strictly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2021-11-02
@che_aa

But there is no new table in pgadmin4

Look in a database called postgres.
In other words, where do you reconnect to this newly created database after CREATE DATABASE private_db?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question