# Executing SQL doesn't work in psql

**URL:** https://meta.discourse.org/t/executing-sql-doesnt-work-in-psql/130054
**Category:** Self-hosting
**Created:** [October 2, 2019, 3:54pm UTC](https://meta.discourse.org/t/executing-sql-doesnt-work-in-psql/130054 "2019-10-02T15:54:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![slackmoehrle](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slackmoehrle/32/82215_2.png) [@slackmoehrle](https://meta.discourse.org/u/slackmoehrle)
#### Post date: [October 2, 2019, 3:54pm UTC](https://meta.discourse.org/t/executing-sql-doesnt-work-in-psql/130054/1 "2019-10-02T15:54:30Z")

</div>

I’m trying to execute:

```sql
SELECT user_id, 0 post_id, current_timestamp granted_at 
FROM badge_posts  
WHERE (:backfill OR user_id IN (:user_ids) OR 0 NOT IN (:post_ids) )
GROUP BY user_id 
HAVING count(*) > 1000

```

but I’m getting an error on :backfill. Can anyone tell me why?

 ![58](https://global.discourse-cdn.com/meta/original/3X/3/9/39c3344a1b8db2263f07c04540549e9a41feed9a.jpeg)

I know this SQL used to run in badge creation.

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [October 2, 2019, 4:13pm UTC](https://meta.discourse.org/t/executing-sql-doesnt-work-in-psql/130054/2 "2019-10-02T16:13:09Z")

</div>

Anything starting with a `:` is a named parameter meant to be replaced by [mini\_sql](https://github.com/discourse/mini_sql) prior to being sent to the DBMS, it’s not valid SQL by itself.

Example, see [2] below:

```plaintext
[1] pry(main)> User.find_by(username: 'anon43915857').id
=> 5

[2] pry(main)> DB.query(
  "SELECT id FROM users WHERE username = :username",
  { username: 'anon43915857' }
).first.id
=> 5

[3] pry(main)> DB.query(
  "SELECT id FROM users WHERE username = ?",
  'anon43915857' 
).first.id
=> 5

```

---

<div class="post-metadata">

### Author: ![slackmoehrle](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slackmoehrle/32/82215_2.png) [@slackmoehrle](https://meta.discourse.org/u/slackmoehrle)
#### Post date: [October 2, 2019, 5:51pm UTC](https://meta.discourse.org/t/executing-sql-doesnt-work-in-psql/130054/3 "2019-10-02T17:51:30Z")

</div>

Thank you. This is clear.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [November 1, 2019, 5:52pm UTC](https://meta.discourse.org/t/executing-sql-doesnt-work-in-psql/130054/4 "2019-11-01T17:52:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
