{"id":1901,"date":"2010-08-30T08:00:50","date_gmt":"2010-08-30T16:00:50","guid":{"rendered":"http:\/\/www.chesnok.com\/daily\/?p=1901"},"modified":"2012-03-26T02:51:57","modified_gmt":"2012-03-26T10:51:57","slug":"variable-substitution-with-psql","status":"publish","type":"post","link":"https:\/\/www.chesnok.com\/daily\/2010\/08\/30\/variable-substitution-with-psql\/","title":{"rendered":"Variable substitution with psql"},"content":{"rendered":"<p><b>Updated:<\/b> Thanks @johto for s\/:bar\/:foo\/. \ud83d\ude42<\/p>\n<p>A coworker asked about variable substitution with psql using <code>\\set<\/code>, and so I looked into it a bit further.<\/p>\n<p>You definitely can do things like this:<br \/>\n<code><br \/>\n16:55 sdeckelmann@[local]:5432|postgres=> \\set test 'select * from :foo limit 10;'<br \/>\n16:56 sdeckelmann@[local]:5432|postgres=> \\set foo 'test'<br \/>\n16:56 sdeckelmann@[local]:5432|postgres=> :test<br \/>\nmyint<br \/>\n-------<br \/>\n     1<br \/>\n     2<br \/>\n     3<br \/>\n     4<br \/>\n     5<br \/>\n     6<br \/>\n     7<br \/>\n     8<br \/>\n     9<br \/>\n    10<br \/>\n(10 rows)<br \/>\n<\/code><\/p>\n<p>But, what about something like this:<\/p>\n<p><code><br \/>\n=> \\set test 'select * from :var limit 10;'<br \/>\n=> :test mytable<br \/>\n<\/code><\/p>\n<p>Unfortunately, this isn&#8217;t supported. <\/p>\n<p>The best you could do is something pathological like:<\/p>\n<p><code>=> \\set s 'select * from '<br \/>\n=> \\set pr ' limit 10;'<br \/>\n=> :s mytable :pr<br \/>\n=> :s test :pr<br \/>\nmyint<br \/>\n-------<br \/>\n     1<br \/>\n     2<br \/>\n     3<br \/>\n     4<br \/>\n     5<br \/>\n     6<br \/>\n     7<br \/>\n     8<br \/>\n     9<br \/>\n    10<br \/>\n(10 rows)<br \/>\n<\/code><br \/>\n<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updated: Thanks @johto for s\/:bar\/:foo\/. \ud83d\ude42 A coworker asked about variable substitution with psql using \\set, and so I looked into it a bit further. You definitely can do things like this: 16:55 sdeckelmann@[local]:5432|postgres=> \\set test &#8216;select * from :foo &hellip; <a href=\"https:\/\/www.chesnok.com\/daily\/2010\/08\/30\/variable-substitution-with-psql\/\">Continue reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97,9],"tags":[339,647,337,338],"class_list":["post-1901","post","type-post","status-publish","format-standard","hentry","category-postgres","category-postgresql","tag-commandline","tag-postgres","tag-psql","tag-variables"],"_links":{"self":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/1901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/comments?post=1901"}],"version-history":[{"count":10,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":3959,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/1901\/revisions\/3959"}],"wp:attachment":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/media?parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/categories?post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/tags?post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}