Skip to content

Instantly share code, notes, and snippets.

@teodorlu
Created December 21, 2019 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teodorlu/29df692b3084852b427ea77b384ace40 to your computer and use it in GitHub Desktop.
Save teodorlu/29df692b3084852b427ea77b384ace40 to your computer and use it in GitHub Desktop.
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