Hi All,
Happy learning :::
Use of PostgreSQL remotly -> It means if you dont have PostgresSQL install on your machine, still you want to create/access database of other machine then after installation of PostgreSQL on remote machine.
Goto - > intallation drtive:\Program Files\PostgreSQL\9.2\data\pg_hba.conf
open it as run as Adminitrator mode
edit on following things:::
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
click on save button.
now use it as ::::
################################
machineIP is remote macine ip address.
################################
import psycopg2
import re
import psycopg2.extras
def nospecial(s):
pattern = re.compile('[^a-zA-Z0-9_]+')
return pattern.sub('', s)
db = "jaona"
conn=psycopg2.connect(user='postgres', password='xp2kal1', host=machineIP, port='5432')
conn.autocommit = True
cursor = conn.cursor()
cursor.execute("CREATE DATABASE %s OWNER %s" % (nospecial(db), nospecial('postgres')))
conn.commit()
cursor.close()
conn.close()
conn_string = "host=machineIP dbname="+db+" user=postgres password=xp2kal1 port=5432"
print conn_string
dbh = psycopg2.connect(conn_string)
cur = dbh.cursor(cursor_factory=psycopg2.extras.DictCursor)
ArgumentTableName = 'test'
sql = "CREATE TABLE if not exists "+ArgumentTableName+" (machineID SERIAL PRIMARY KEY,COMP_DATA text,STREAM text,XML_PATH_VER_X text,XML_PATH_VER_Y text)"
try:
cur.execute(sql)
cur.execute("INSERT INTO "+ArgumentTableName+"(COMP_DATA, STREAM, XML_PATH_VER_X, XML_PATH_VER_Y) VALUES ('%s', '%s', '%s', '%s')" %
("aa","aaa","asd","sdd"))
dbh.commit()
except Exception,e:
print "eeee ::: " ,e
finally:
cur.close()
conn.close()
if dbh:
dbh.close()
#To delete database#
def deleteDB(db):
with psycopg2.connect(host=machineIP,database="postgres", user="postgres", password="xp2kal1", port='5432') as conn:
with conn.cursor() as cur:
conn.autocommit = True # Explains why we do this - we cannot drop or create from within a DB transaction. http://initd.org/psycopg/docs/connection.html#connection.autocommit
cur.execute("DROP DATABASE "+nospecial(db)+";")
deleteDB(db)
Happy learning :::
Use of PostgreSQL remotly -> It means if you dont have PostgresSQL install on your machine, still you want to create/access database of other machine then after installation of PostgreSQL on remote machine.
Goto - > intallation drtive:\Program Files\PostgreSQL\9.2\data\pg_hba.conf
open it as run as Adminitrator mode
edit on following things:::
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
click on save button.
now use it as ::::
################################
machineIP is remote macine ip address.
################################
import psycopg2
import re
import psycopg2.extras
def nospecial(s):
pattern = re.compile('[^a-zA-Z0-9_]+')
return pattern.sub('', s)
db = "jaona"
conn=psycopg2.connect(user='postgres', password='xp2kal1', host=machineIP, port='5432')
conn.autocommit = True
cursor = conn.cursor()
cursor.execute("CREATE DATABASE %s OWNER %s" % (nospecial(db), nospecial('postgres')))
conn.commit()
cursor.close()
conn.close()
conn_string = "host=machineIP dbname="+db+" user=postgres password=xp2kal1 port=5432"
print conn_string
dbh = psycopg2.connect(conn_string)
cur = dbh.cursor(cursor_factory=psycopg2.extras.DictCursor)
ArgumentTableName = 'test'
sql = "CREATE TABLE if not exists "+ArgumentTableName+" (machineID SERIAL PRIMARY KEY,COMP_DATA text,STREAM text,XML_PATH_VER_X text,XML_PATH_VER_Y text)"
try:
cur.execute(sql)
cur.execute("INSERT INTO "+ArgumentTableName+"(COMP_DATA, STREAM, XML_PATH_VER_X, XML_PATH_VER_Y) VALUES ('%s', '%s', '%s', '%s')" %
("aa","aaa","asd","sdd"))
dbh.commit()
except Exception,e:
print "eeee ::: " ,e
finally:
cur.close()
conn.close()
if dbh:
dbh.close()
#To delete database#
def deleteDB(db):
with psycopg2.connect(host=machineIP,database="postgres", user="postgres", password="xp2kal1", port='5432') as conn:
with conn.cursor() as cur:
conn.autocommit = True # Explains why we do this - we cannot drop or create from within a DB transaction. http://initd.org/psycopg/docs/connection.html#connection.autocommit
cur.execute("DROP DATABASE "+nospecial(db)+";")
deleteDB(db)
No comments:
Post a Comment