Guest User

Scripts for setting up a PostgreSQL db and a test script

a guest
Feb 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. ###pg_setup.sh starts###
  2. #!/bin/bash
  3.  
  4. mkdir /usr/local/pgsql
  5. chown postgres /usr/local/pgsql
  6. su postgres
  7. pg_ctl -D /usr/local/pgsql/data initdb
  8.  
  9. /usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
  10. ECHO "Click enter to stop"
  11. read
  12. /usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
  13. ###pg_setup.sh ends###
  14.  
  15. #remember to change the postgress pw and create the "cfh" db
  16. --
  17. sudo -u postgres psql cfh
  18. #in psql:
  19. ALTER USER "postgres" PASSWORD 'cfh';
  20. --
  21.  
  22. ###tmp_db_connect_test.py starts###
  23. #!/usr/bin/env python
  24. # -*- coding: utf-8 -*-
  25.  
  26. import psycopg2
  27.  
  28. import sys
  29.  
  30. db_con = None
  31.  
  32. try:
  33.  
  34. #Create a database session
  35.  
  36. db_con = psycopg2.connect(database='cfh', user='postgres', password='cfh')
  37.  
  38. #Create a client cursor to execute commands
  39.  
  40. VAL1 = '1' * 20
  41. VAL2 = '2' * 20
  42. #print(VAL1, type(VAL1))
  43. cursor = db_con.cursor()
  44.  
  45. #Uncomment and comment lines to test different operations
  46. #Note that requests table is gimped down to just two unique columns
  47. cursor.execute("DROP TABLE Requests;")
  48. cursor.execute("CREATE TABLE Requests (id SERIAL PRIMARY KEY UNIQUE, service_request_id VARCHAR UNIQUE, status_notes VARCHAR NULL, status VARCHAR, service_code VARCHAR, service_name VARCHAR, description VARCHAR, agency_responsible VARCHAR, service_notice VARCHAR, requested_datetime DATE, updated_datetime DATE, expected_datetime DATE, address_string VARCHAR, media_url VARCHAR, distance VARCHAR, location BYTEA, service_object_id VARCHAR, title VARCHAR, service_object_type VARCHAR, detailed_status VARCHAR, api_key VARCHAR, email VARCHAR, first_name VARCHAR, last_name VARCHAR, phone VARCHAR);")
  49. #Add stuff
  50. cursor.execute("INSERT INTO Requests ( service_request_id) VALUES (%s);" % VAL1)
  51. cursor.execute("INSERT INTO Requests ( service_request_id) VALUES (%s);" % VAL2)
  52.  
  53. #Comment this line to test for errors, uncomment to change the database content
  54. db_con.commit()
  55.  
  56.  
  57. cursor.execute("SELECT * FROM Requests")
  58. #fetches only one
  59. print(cursor.fetchone())
  60. except psycopg2.DatabaseError as e:
  61.  
  62. print ('Error: %s' % e)
  63.  
  64. sys.exit(1)
  65.  
  66. finally:
  67.  
  68. if db_con:
  69.  
  70. db_con.close()
  71. ###tmp_db_connect_test.py starts###
Add Comment
Please, Sign In to add comment