2

Do you know if it's possible to create a role/user for the Postgresql database from within Python code?

I would prefer to use asyncpg library, since my program is based on asynchronous code, but if there are better libraries for this specific task, I don't mind using them.

I already have a pre-installed database on my server machine, so another possibility would be to just run the Shell command from withing the Python program to create a role. However, I am not sure if you can create a role in just one Shell line.

4
  • maybe with psycopg2 Commented Dec 10, 2019 at 17:35
  • @GiovaniSalazar How would the CREATE ROLE query look like there? And do you know for sure that psycopg2 supports role creations? I couldn't find definitive answers in their docs. Commented Dec 10, 2019 at 17:36
  • psycopg2 is a client for python ...here there is an example : stackoverflow.com/questions/53022587/… Commented Dec 10, 2019 at 17:39
  • @GiovaniSalazar Thanks! I think I found an even more elegant 1-line solution to my problem: pool.execute("CREATE ROLE name ...") Commented Dec 10, 2019 at 17:50

1 Answer 1

1

After some digging, the answer appeared to be very straightforward:

pool.execute("CREATE ROLE name ...")

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.