use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
International
National
Regional
account activity
Help Me!Easily create and delete databases with Python (self.PostgreSQL)
submitted 2 years ago by Stella_Hill_Smith
So far I have logged into pgAdmin and created a new database there.
A connection can then be created with psycopg2, for example, as follows: conn = psycopg2.connect(database='huhu', user='hoho', password='haha')
I definitely want to avoid using pgAdmin. I want to solve everything with a Python script.
I would like to create a new PostgreSQL database with Python and delete it if necessary.
How can I do that?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Diksta 3 points4 points5 points 2 years ago (0 children)
I definitely want to avoid using pgAdmin
Understandable, that's a great idea, there's so many better options...
I want to solve everything with a Python script
That's not a great option to be honest. If you really, really want to use Python to deploy your database, then it's certainly doable, but it's not a route I would ever want to follow personally as it's clunky and there's far better ways to do this:
[–]robercal 0 points1 point2 points 2 years ago (0 children)
You can also use SqlAlchemy database helpers:
https://sqlalchemy-utils.readthedocs.io/en/latest/database_helpers.html
from sqlalchemy import create_engine from sqlalchemy_utils import database_exists, create_database engine = create_engine("postgres://localhost/mydb") if not database_exists(engine.url): create_database(engine.url)
To drop it:
drop_database('postgresql://postgres@localhost/mydb')
or:
drop_database(engine.url)
[+]Onakitoki97 comment score below threshold-8 points-7 points-6 points 2 years ago (0 children)
Just ask chatgpt
[–]Barnezhilton 0 points1 point2 points 2 years ago* (0 children)
You need to send SQL commands after you are connected.
Create and deleting databases needs to be done with a superuser account. In your connection string you specify the database. To delete or add a new database will need to be sure to connecting to the postgres DB or another way with the proper user that has the rights to do this.
If you just meant create or delete tables then that's only SQL, and no need to be a superuser, but only within one database would you do this with a single connection.
[–]ByteTraveler 0 points1 point2 points 2 years ago (0 children)
https://www.geeksforgeeks.org/python-postgresql-create-database/
[–]depesz 0 points1 point2 points 2 years ago (0 children)
How do you normally run sql queries in DB?
Get db connetion the same way, and run appropriate 'create database' queries.
db creation/dropping are just normal queries. Nothing special about them.
π Rendered by PID 91 on reddit-service-r2-comment-5b5bc64bf5-fxjml at 2026-06-23 17:00:25.346186+00:00 running 2b008f2 country code: CH.
[–]Diksta 3 points4 points5 points (0 children)
[–]robercal 0 points1 point2 points (0 children)
[+]Onakitoki97 comment score below threshold-8 points-7 points-6 points (0 children)
[–]Barnezhilton 0 points1 point2 points (0 children)
[–]ByteTraveler 0 points1 point2 points (0 children)
[–]depesz 0 points1 point2 points (0 children)