Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

PostgreSQL SQL dialect

postgres > select all custom plpgsql functions

1
2
3
4
5
6
7
8
select 
  pp.proname, 
  pg_get_functiondef(pp.oid)
from pg_proc pp
inner join pg_namespace pn on (pp.pronamespace = pn.oid)
inner join pg_language pl on (pp.prolang = pl.oid)
where pl.lanname IN ('plpgsql') 
  and pn.nspname <> 'information_schema';
https://snipt.net/embed/b0d97086d193266305bee10c8f8adbc4/
https://snipt.net/raw/b0d97086d193266305bee10c8f8adbc4/
b0d97086d193266305bee10c8f8adbc4
postgresql
PostgreSQL SQL dialect
8
2016-12-09T23:20:10
True
False
False
/api/public/snipt/148005/
postgres-select-all-custom-functions
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">select</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">pp</span><span class="mf">.</span><span class="n">proname</span><span class="p">,</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">pg_get_functiondef</span><span class="p">(</span><span class="n">pp</span><span class="mf">.</span><span class="n">oid</span><span class="p">)</span> </span><span id="L-4"><a name="L-4"></a><span class="k">from</span> <span class="n">pg_proc</span> <span class="n">pp</span> </span><span id="L-5"><a name="L-5"></a><span class="k">inner</span> <span class="k">join</span> <span class="n">pg_namespace</span> <span class="n">pn</span> <span class="k">on</span> <span class="p">(</span><span class="n">pp</span><span class="mf">.</span><span class="n">pronamespace</span> <span class="o">=</span> <span class="n">pn</span><span class="mf">.</span><span class="n">oid</span><span class="p">)</span> </span><span id="L-6"><a name="L-6"></a><span class="k">inner</span> <span class="k">join</span> <span class="n">pg_language</span> <span class="n">pl</span> <span class="k">on</span> <span class="p">(</span><span class="n">pp</span><span class="mf">.</span><span class="n">prolang</span> <span class="o">=</span> <span class="n">pl</span><span class="mf">.</span><span class="n">oid</span><span class="p">)</span> </span><span id="L-7"><a name="L-7"></a><span class="k">where</span> <span class="n">pl</span><span class="mf">.</span><span class="n">lanname</span> <span class="k">IN</span> <span class="p">(</span><span class="s1">&#39;plpgsql&#39;</span><span class="p">)</span> </span><span id="L-8"><a name="L-8"></a> <span class="k">and</span> <span class="n">pn</span><span class="mf">.</span><span class="n">nspname</span> <span class="o">&lt;&gt;</span> <span class="s1">&#39;information_schema&#39;</span><span class="p">;</span> </span></pre></div> </td></tr></table>
postgres, postgresql, psql

Text only

PostgreSQL: Create a user and a database, set as owner, and grant privileges.

1
2
3
CREATE USER bob WITH PASSWORD 'bazzy';
CREATE DATABASE baz WITH OWNER bob;
GRANT ALL PRIVILEGES ON DATABASE baz to foo;
https://snipt.net/embed/77daad8174ebf6c04b747a8d2617ce17/
https://snipt.net/raw/77daad8174ebf6c04b747a8d2617ce17/
77daad8174ebf6c04b747a8d2617ce17
text
Text only
3
2016-12-10T07:42:10
True
False
False
Dec 17, 2015 at 02:24 PM
/api/public/snipt/140056/
postgresql-create-a-user-and-a-database-set-as-owner-and-grant-privileges-5a568550
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>CREATE USER bob WITH PASSWORD &#39;bazzy&#39;; </span><span id="L-2"><a name="L-2"></a>CREATE DATABASE baz WITH OWNER bob; </span><span id="L-3"><a name="L-3"></a>GRANT ALL PRIVILEGES ON DATABASE baz to foo; </span></pre></div> </td></tr></table>
postgres, postgresql, psql

Bash

psql > list databases

$ psql -l
https://snipt.net/embed/235856f12c616a3577752c1c5d7d09bc/
https://snipt.net/raw/235856f12c616a3577752c1c5d7d09bc/
235856f12c616a3577752c1c5d7d09bc
bash
Bash
1
2016-12-09T11:52:43
True
False
False
/api/public/snipt/136197/
psql-list-databases-8ad5d699
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>$ psql -l </span></pre></div> </td></tr></table>
database, postgres, postgresql, psql

Text only

VirtualEnv - Installing Psycopg

1
2
3
4
5
6
If you are using virtualenv you need install psycopg2 inside the environment, something like:

pip install psycopg2 
but you need install some packages with apt before that:

sudo apt-get install python-dev postgresql-server-dev-all
https://snipt.net/embed/b002b3de5bf998ff5d7a3257ed45661b/
https://snipt.net/raw/b002b3de5bf998ff5d7a3257ed45661b/
b002b3de5bf998ff5d7a3257ed45661b
text
Text only
6
2016-12-09T09:56:55
True
False
False
/api/public/snipt/45428/
virtualenv-installing-psycopg
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>If you are using virtualenv you need install psycopg2 inside the environment, something like: </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a>pip install psycopg2 </span><span id="L-4"><a name="L-4"></a>but you need install some packages with apt before that: </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a>sudo apt-get install python-dev postgresql-server-dev-all </span></pre></div> </td></tr></table>
django, postgres, virtualenv

Bash

Create Postgres User

createuser -P -d -a -e joe
https://snipt.net/embed/a80ead577ff66b928efbc5f4e75ffb78/
https://snipt.net/raw/a80ead577ff66b928efbc5f4e75ffb78/
a80ead577ff66b928efbc5f4e75ffb78
bash
Bash
1
2016-12-04T07:17:15
True
False
False
/api/public/snipt/45426/
create-postgres-user-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>createuser -P -d -a -e joe </span></pre></div> </td></tr></table>
postgres

Bash

dump postgres schema

1
2
3
4
5
#dump postgres
su - postgres -c "/usr/bin/pg_dump dbname | gzip -c > /path/dbname.sql.gz"

#dump only schema
su - postgres -c "/usr/bin/pg_dump -s dbname | gzip -c > /path/dbname_schema.sql.gz"
https://snipt.net/embed/869683e174a385507555fd0ec84d98a0/
https://snipt.net/raw/869683e174a385507555fd0ec84d98a0/
869683e174a385507555fd0ec84d98a0
bash
Bash
5
2016-12-09T02:41:59
True
False
False
/api/public/snipt/27810/
dump-postgres-schema
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#dump postgres</span> </span><span id="L-2"><a name="L-2"></a>su - postgres -c <span class="s2">&quot;/usr/bin/pg_dump dbname | gzip -c &gt; /path/dbname.sql.gz&quot;</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">#dump only schema</span> </span><span id="L-5"><a name="L-5"></a>su - postgres -c <span class="s2">&quot;/usr/bin/pg_dump -s dbname | gzip -c &gt; /path/dbname_schema.sql.gz&quot;</span> </span></pre></div> </td></tr></table>
backup, db, dump, postgres
Copyrighted, illegal, or inappropriate content? Email [email protected].