Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quickly get started with a new database + user
#!/usr/bin/env python3
from pprint import pprint
from random import randint
import sys
# Database name required
def print_err(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
if len(sys.argv) < 2:
print_err("Usage:")
print_err(" createcomamnds DATABASE_NAME")
sys.exit(1)
db_name = sys.argv[1]
db_user = sys.argv[1]
hex_num2letter = {
0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
5: '5', 6: '6', 7: '7', 8: '8', 9: '9',
10: 'a', 11: 'b', 12: 'c', 13: 'd',
14: 'e', 15: 'f'
}
def random_hex(count=40):
return "".join(
[hex_num2letter[randint(0, 15)]
for x in range(count)]
)
db_pass = random_hex(40)
print(f"""
Follow the steps below to create a new database {db_name} and a
user.
1. Create the user and the database
$ sudo -u postgres createdb {db_name}
$ sudo -u postgres createuser {db_user}
2. Set the password for the user
$ sudo -u postgres psql
postgres=# \\password {db_user}
Enter new password: {db_pass}
3. Connect with one of the following connection strings:
"postgresql://localhost/{db_name}?user={db_user}&password={db_pass}\"
"jdbc:postgresql://localhost/{db_name}?user={db_user}&password={db_pass}\"
""".strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.