Began to learn Python 2.7 + PostgreSQL 9.6.1. Python already has some experience, but I don’t have any experience with PostgreSQL. Is it possible to create a database with connections at all?

I create a database with the following code, but how to add tables to it (with the definition of data types) and the relations between them:

#!/usr/bin/python # -*- coding: utf-8 -*- from psycopg2 import connect import sys from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT con = None con = connect(user='postgres', password='poilk', port=5432) dbname = "name_db" con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cur = con.cursor() cur.execute('CREATE DATABASE ' + dbname) cur.close() con.close() 

For example: how to create in Python 2 tables with a one-to-many relationship in the database:

Table 1 - user_name (ID, name, password)

Table 2 - additionally (age, sex, hp, items, knowledge)

  • Learn materiel. PostgreSQL Documentation - aleks.andr
  • Please correct the question, it is not clear what answer you want to receive. by mistake, you already have a database named name_db, which the console tells you. the code in the first block is correct, but it only creates the base - nothing more, I suspect that it does something else further. the solution is to remove the database manually or check the presence of the database in the first block and delete it, passing the command through execute (). - while1pass
  • on the question at the end - it does not really refer to the python. Yes, the Python library is used, but it is only needed to transfer commands from the off-documentation postgresql.org/docs/9.6/static/sql-createtable.html using execute () - while1pass
  • What is your question: how to create a table using psycopg2 ? Are you interested in which SQL query you need to write to create a table? Then ask about this (remove everything else). If you know how to create a table, then give a sample code for the two specified tables. How to express a one-to-many relation in SQL (a dialect supported by Postgres) for the specified tables is a separate question. (By the way, describe what is connected with what: do you want one user to be associated with several characters, with given ages, sex, etc. be connected?) - jfs

0