How to use SQL to query discourse databse


#1

This is a follow on question to one I had about finding who took part in a poll. I was told that I could get this with SQL query, but I’m not clear on how or where I can run this query.

Is there an adhoc query screen in discourse? If not, can someone point me in right direction as to where I can run SQL again discourses database.

Thanks


(Jeff Atwood) #2

@riking is working on a read only query plugin.


Connect to pg database from console in Discourse installation?
#3

Any eta on that new plugin? Thanks


(Jeff Atwood) #4

no, but if you would like to speed up the work, may I suggest one of our hosting plans http://discourse.org/buy :wink:


#5

@codinghorror I wish I was in a position to do so, hopefully one day :smile:


(Gerhard Schlager) #6

There’s no convenient way to do this at the moment. But there are already a few topics about connecting to the PostgreSQL database. Try searching here on meta. As a starting point:


(Rafael dos Santos Silva) #7
./launcher enter app
su discourse
psql
SELECT * FROM users LIMIT 10;
```

:smile:

(SMHassanAlavi) #8

I want to run a query in plugin.rb in my plugin.
I took a look at THIS sql query. But it just run query in User activeRecord.
I want to run every sql in plugin.rb. for exmaple I tested this code in console rails db:
select * from category_users where category_id = 50
and I received a result but I don’t khow How to run this query in plugin.rb. How to change the result into a json?


(Mittineague) #9

Not the best example, but hopefully enough to give you the idea.


(SMHassanAlavi) #10

Thank you but I found this:
sql = "select * from category_users where category_id = #{category_id}" result = ActiveRecord::Base.connection.execute(sql)
What about this? Is it a good syntax?