× Main Home Install Configuration Query
☰ PostgreSQL

PostgreSQL

PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley

Installing PostgreSQL in Ubuntu
Q:Installing PostgreSQL

sudo apt update


sudo apt install postgresql postgresql-contrib

Q:One way is to switch over to the postgres account on your server by typing:


sudo -i -u postgres


Q:Then you can access the Postgres prompt by typing:

psql


Q:To exit out of the PostgreSQL prompt, run the following:

\q


Q:Another way to connect to the Postgres prompt is to run the psql command as the postgres account directly with sudo:

sudo -u postgres psql


Q:Creating a New Role

createuser --interactive

sudo -u postgres createuser --interactive


Q:Change USER Password

ALTER USER postgres PASSWORD 'newPassword';


......................................................
For more info click here
......................................................
DB Configuration Python

CONNECTION
........................

def db_connect():
   try:
     conn = psycopg2.connect(database="wahni",
     user="postgres",
     password="jobin",
     host="127.0.0.1",
     port="5432")
     cur = conn.cursor()
   except (Exception, psycopg2.DatabaseError) as error:
     print("Error while connecting to PostgreSQL table", error)
  return conn, cur
DB Query Python

QUERY
........................

def db_query():
  conn, cur = db_connect()
  try:
    cur.execute('SELECT * FROM tharif1')
    records = cur.fetchall()
    for row in records:
      print(row[0]
  except Exception as e:
    print('error', e)
  finally:
    conn.commit()
    cur.close()
    conn.close()