{"id":8359,"date":"2023-06-29T09:46:42","date_gmt":"2023-06-29T14:46:42","guid":{"rendered":"https:\/\/bobbeaty.com\/wp\/?p=8359"},"modified":"2023-07-02T08:14:51","modified_gmt":"2023-07-02T13:14:51","slug":"nice-postgresql-trick","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/8359","title":{"rendered":"Nice Postgresql Trick"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2008\/05\/postgresql.jpg\" alt=\"PostgreSQL.jpg\" title=\"PostgreSQL.jpg\" border=\"0\" width=\"128\" height=\"128\" style=\"float:right; margin-left:10px;\" \/><\/p>\n<p>This morning I really wanted to be able to set the <tt>psql<\/tt> prompt in my sessions because the system we have for creating databases doesn't really create nicely <em>human-readable<\/em> name, and even so, I'd like to have the name in the prompt match the branch of the code I'm working on... it just works out better.<\/p>\n<p>So I started digging, and the <tt>-c<\/tt> parameter is OK, but the <tt>psql<\/tt> session terminates after that - so that's not going to work. And piping in the <tt>\\set<\/tt> commands seemed to be problematic, and then I found this:<\/p>\n<pre class=\"bash\" style=\"font-family:monospace;\">  $ psql <span style=\"color: #660033;\">--set<\/span>=<span style=\"color: #007800;\">PROMPT1<\/span>=<span style=\"color: #ff0000;\">&quot;<span style=\"color: #007800;\">${br}<\/span>%R%#%x &quot;<\/span><\/pre>\n<p>where <tt>br<\/tt> is the name of the branch I'm working on. This could then be obtained from <tt>git<\/tt> easily, and then put into a function, and it works great!<\/p>\n<pre class=\"bash\" style=\"font-family:monospace;\">  <span style=\"color: #666666; font-style: italic;\">#<\/span>\n  <span style=\"color: #666666; font-style: italic;\"># &quot;platter psql (postgres command line tool)&quot; - start a psql session on<\/span>\n  <span style=\"color: #666666; font-style: italic;\"># the provided branch in the PLATTER_INSTANCE in the .env file in the<\/span>\n  <span style=\"color: #666666; font-style: italic;\"># current directory (a Node project repo). If no branch is provided, then<\/span>\n  <span style=\"color: #666666; font-style: italic;\"># the current git branch will be used.<\/span>\n  <span style=\"color: #666666; font-style: italic;\">#<\/span>\n  <span style=\"color: #000000; font-weight: bold;\">function<\/span> ppsql<span style=\"color: #7a0874; font-weight: bold;\">&#40;<\/span><span style=\"color: #7a0874; font-weight: bold;\">&#41;<\/span> <span style=\"color: #7a0874; font-weight: bold;\">&#123;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #7a0874; font-weight: bold;\">&#91;<\/span> <span style=\"color: #660033;\">-f<\/span> .env <span style=\"color: #7a0874; font-weight: bold;\">&#93;<\/span>; <span style=\"color: #000000; font-weight: bold;\">then<\/span>\n      <span style=\"color: #000000; font-weight: bold;\">set<\/span> <span style=\"color: #660033;\">-o<\/span> allexport; <span style=\"color: #7a0874; font-weight: bold;\">source<\/span> .env; <span style=\"color: #000000; font-weight: bold;\">set<\/span> +o allexport\n      <span style=\"color: #7a0874; font-weight: bold;\">local<\/span> <span style=\"color: #007800;\">br<\/span>=<span style=\"color: #007800;\">$1<\/span>\n      <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #7a0874; font-weight: bold;\">&#91;<\/span> <span style=\"color: #800000;\">${#br}<\/span> <span style=\"color: #660033;\">-eq<\/span> <span style=\"color: #000000;\">0<\/span> <span style=\"color: #7a0874; font-weight: bold;\">&#93;<\/span>; <span style=\"color: #000000; font-weight: bold;\">then<\/span>\n        <span style=\"color: #007800;\">br<\/span>=<span style=\"color: #000000; font-weight: bold;\">`<\/span><span style=\"color: #c20cb9; font-weight: bold;\">cat<\/span> .git<span style=\"color: #000000; font-weight: bold;\">\/<\/span>HEAD <span style=\"color: #000000; font-weight: bold;\">|<\/span> <span style=\"color: #c20cb9; font-weight: bold;\">sed<\/span> <span style=\"color: #660033;\">-e<\/span> <span style=\"color: #ff0000;\">'s:^.*\/::'<\/span><span style=\"color: #000000; font-weight: bold;\">`<\/span>\n      <span style=\"color: #000000; font-weight: bold;\">fi<\/span>\n      <span style=\"color: #7a0874; font-weight: bold;\">local<\/span> <span style=\"color: #007800;\">url<\/span>=<span style=\"color: #ff0000;\">&quot;<span style=\"color: #780078;\">`npx platter postgres branch url ${br} \\\n           --instance $PLATTER_INSTANCE | tr -d '\\n'`<\/span>?sslmode=require&quot;<\/span>\n      psql <span style=\"color: #660033;\">--set<\/span>=<span style=\"color: #007800;\">PROMPT1<\/span>=<span style=\"color: #ff0000;\">&quot;<span style=\"color: #007800;\">${br}<\/span>%R%#%x &quot;<\/span> <span style=\"color: #660033;\">--set<\/span>=<span style=\"color: #007800;\">PROMPT2<\/span>=<span style=\"color: #ff0000;\">&quot;<span style=\"color: #007800;\">${br}<\/span>%R%#%x &quot;<\/span> <span style=\"color: #007800;\">$url<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">else<\/span>\n      <span style=\"color: #7a0874; font-weight: bold;\">echo<\/span> <span style=\"color: #ff0000;\">&quot;Not in a Node project directory!&quot;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">fi<\/span>\n  <span style=\"color: #7a0874; font-weight: bold;\">&#125;<\/span><\/pre>\n<p>With this, it's so easy now to be able to keep track of the database (branch) I'm on with Platter, and that makes a really big different to my peace of mind. \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This morning I really wanted to be able to set the psql prompt in my sessions because the system we have for creating databases doesn&#8217;t really create nicely human-readable name, and even so, I&#8217;d like to have the name in the prompt match the branch of the code I&#8217;m working on&#8230; it just works out [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-8359","post","type-post","status-publish","format-standard","hentry","category-coding","category-open-source-software"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/8359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=8359"}],"version-history":[{"count":2,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/8359\/revisions"}],"predecessor-version":[{"id":8361,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/8359\/revisions\/8361"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=8359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=8359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=8359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}