Is there any convenience method like engine.interactive() that just drops you into a CLI in the current database, similar to breakpoint() dropping you into a PDB? One thing I considered was
subprocess.run(['psql', engine.url])
But that doesn't work since psql doesn't accept the +psycopg2: bit of the sqlalchemy connection string.
$ psql postgresql+psycopg2://postgres:dev@localhost:5432/
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist
$ psql postgresql://postgres:dev@localhost:5432/
psql (16.4 (Debian 16.4-1.pgdg120+1))
Type "help" for help.
postgres=#
I can slice it off myself and this works:
def enter_cli(engine):
url = engine.url.set(drivername=url.drivername.split("+")[0])
connection_string = url.render_as_string(hide_password=False)
subprocess.run(["psql", connection_string])
But that seems likely to be non-portable, as I'm hardcoding the CLI and the specific type of connection string. (e.g. it looks like pgcli wants postgres:// and doesn't accept postgresql://.) The other approach would be writing a python-based CLI, but with the naive approach you lose a lot of tab-completion and similar features. Is there a portable way to do this?
0 comments:
Post a Comment
Thanks