Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import random
- import string
- def random_word():
- return ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(random.randint(5, 15)))
- def rand_gender():
- gender = random.randint(0,1)
- genders = { '0' : "male", '1' : "female"}
- return genders[str(gender)]
- def main():
- conn_string = "host='localhost' dbname='postgres' user='postgres' password='postgres'"
- print("Connecting to database\n ->%s" % (conn_string))
- conn = psycopg2.connect(conn_string)
- cursor = conn.cursor()
- cursor.execute("CREATE TABLE IF NOT EXISTS users (id INT NOT NULL, first_name VARCHAR, last_name VARCHAR, email VARCHAR, gender VARCHAR)")
- print("Connected!\n")
- for i in range(1000000):
- qs = "INSERT INTO users (id, first_name, last_name, email, gender) VALUES "
- query_string = "('{0}', '{1}', '{2}', '{3}@{4}.com', '{5}')"
- query = query_string.format(i, random_word(), random_word(), random_word(), random_word(), rand_gender())
- qs = qs + query
- if i%1000 == 0: print (str(i) + "row is added")
- res = cursor.execute(qs)
- conn.commit()
- cursor.close()
- conn.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement