xeus-sql icon indicating copy to clipboard operation
xeus-sql copied to clipboard

Possible to execute the 'slash' commands on postgres? \d \dt \list

Open jameshowison opened this issue 3 years ago • 3 comments

Postgres uses commands starting with a \ char, but these don't seem to execute in xues-sql. e.g.,

\list

which should show all databases the user can see on the server, but it gives this error:

Error: Cannot execute query. Fatal error. ERROR:  syntax error at or near "\"
LINE 1: \list
        ^
 while executing "\list".

Same for the commonly used \d and \dt.

jameshowison avatar Feb 04 '22 21:02 jameshowison

Hi @jameshowison, thanks for opening the issue. Currently xeus-sql doesn't have a roadmap for implementing something like this.

But in case you or anyone would like to tackle this issue, here are some tips:

  • The jupyter protocol calls this kind of operation "magics" and they start with a % sign instead
  • There's a fair number of examples on how to implement these using sqlite on the xeus-sqlite repo, see and it's not too hard to implement the exact same logic here
  • For postgres or any other flavor of SQL the person implement these magics would have to look into SOCI api and call and use the methods correctly, attributing them to each one of the magics they'd create while parsing the user input

If you need any help, feel free to open issues here or contact me on a synchronous way.

marimeireles avatar Feb 08 '22 22:02 marimeireles

Thanks for the guidance. I don't think I can implement that, but I am trying to bring it someone's attention who might be able to.

Just for additional insight, one of the reasons that this is crucial is that one cannot switch databases in xeus-sql, one has to know to reconnect to change databases. That's because that requires the \c command: https://stackoverflow.com/questions/3949876/how-to-switch-databases-in-psql

The consequence of this is that currently in xeus-sql if you connect and use CREATE DATABASE some_db then issue a CREATE TABLE some_table command, the table is actually created in the default database and not in the new database. In my case the default database was created named after the database user. I could only find this out using the commandline client and pgadmin.

So one would have to do:

%LOAD postgresql host=localhost
CREATE DATABASE some_db;

then reconnect to change to that database.

%LOAD postgresql host=localhost dbname=some_db

then create the tables:

CREATE TABLE some_table

I guess once one knows this it's not too bad :)

jameshowison avatar Feb 17 '22 15:02 jameshowison

If SOCI doesn't offer this kind of stuff we can encapsulate this nicely by calling %LOAD postgresql host=localhost dbname=some_db in our API and some_db is the %\c argument. Thanks for the insights for this one @jameshowison! :)

marimeireles avatar Feb 21 '22 13:02 marimeireles