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-09T08:16:57
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

Bash

Export PostgreSQL schema only (no data) and discard ownership

pg_dump --schema-only --no-owner -h {host} -U {user} -f {filename.sql} {database-name}
https://snipt.net/embed/e552972d1b42c3d6f5e1a796a7d3355c/
https://snipt.net/raw/e552972d1b42c3d6f5e1a796a7d3355c/
e552972d1b42c3d6f5e1a796a7d3355c
bash
Bash
2
2016-12-06T13:19:10
True
False
False
Sep 01, 2016 at 04:22 PM
/api/public/snipt/147682/
export-postgresql-schema-only-no-data-and-discard-ownership
<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>pg_dump --schema-only --no-owner -h <span class="o">{</span>host<span class="o">}</span> -U <span class="o">{</span>user<span class="o">}</span> -f <span class="o">{</span>filename.sql<span class="o">}</span> <span class="o">{</span>database-name<span class="o">}</span> </span></pre></div> </td></tr></table>
backup, postgresql

PostgreSQL console (psql)

Dump data from postgreSQL and compress it via gzip

psql -d news -c "\copy (select * from cnyes where newstime between '2015-1-1' and '2016-3-31') to stdout WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *)"  | gzip > /home/aha/cnyes.csv.gz
https://snipt.net/embed/7ecf3e734986d2ffd64027234dc64bac/
https://snipt.net/raw/7ecf3e734986d2ffd64027234dc64bac/
7ecf3e734986d2ffd64027234dc64bac
psql
PostgreSQL console (psql)
1
2016-12-09T15:58:53
True
False
False
Apr 09, 2016 at 08:30 AM
/api/public/snipt/145288/
dump-data-from-postgresql-and-compress-it-via-gzip
<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><span class="n">psql</span> <span class="o">-</span><span class="n">d</span> <span class="n">news</span> <span class="o">-</span><span class="n">c</span> <span class="s s-Name">&quot;\copy (select * from cnyes where newstime between &#39;2015-1-1&#39; and &#39;2016-3-31&#39;) to stdout WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *)&quot;</span> <span class="o">|</span> <span class="n">gzip</span> <span class="o">&gt;</span> <span class="o">/</span><span class="n">home</span><span class="o">/</span><span class="n">aha</span><span class="o">/</span><span class="n">cnyes</span><span class="mf">.</span><span class="k">csv</span><span class="mf">.</span><span class="n">gz</span> </span></pre></div> </td></tr></table>
csv, gzip, postgresql, sql

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-08T23:18:54
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

SQL

Delete Database That Has Active Connections

1
2
3
SELECT * FROM pg_stat_activity WHERE datname = '<database_name>';
SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '<database_name>';
DROP DATABASE '<database_name>'
https://snipt.net/embed/26717b31e0878d6cd8dbedee6492e2e4/
https://snipt.net/raw/26717b31e0878d6cd8dbedee6492e2e4/
26717b31e0878d6cd8dbedee6492e2e4
sql
SQL
3
2016-12-04T07:20:23
True
False
False
Mar 10, 2014 at 10:02 PM
/api/public/snipt/120719/
delete-database-that-has-active-connections
<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><span class="k">SELECT</span> <span class="o">*</span> <span class="k">FROM</span> <span class="n">pg_stat_activity</span> <span class="k">WHERE</span> <span class="n">datname</span> <span class="o">=</span> <span class="s1">&#39;&lt;database_name&gt;&#39;</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="k">SELECT</span> <span class="n">pg_terminate_backend</span> <span class="p">(</span><span class="n">pg_stat_activity</span><span class="p">.</span><span class="n">pid</span><span class="p">)</span> <span class="k">FROM</span> <span class="n">pg_stat_activity</span> <span class="k">WHERE</span> <span class="n">pg_stat_activity</span><span class="p">.</span><span class="n">datname</span> <span class="o">=</span> <span class="s1">&#39;&lt;database_name&gt;&#39;</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="k">DROP</span> <span class="k">DATABASE</span> <span class="s1">&#39;&lt;database_name&gt;&#39;</span> </span></pre></div> </td></tr></table>
postgresql

PostgreSQL SQL dialect

PostgreSQL :: Active connections in PostgreSQL

1
2
3
4
5
SELECT  t1.* 
   FROM PG_STAT_ACTIVITY t1
;

-- En MySql sería: SHOW PROCESSSLIST
https://snipt.net/embed/7765c84277968b143bbb284eae86d0b2/
https://snipt.net/raw/7765c84277968b143bbb284eae86d0b2/
7765c84277968b143bbb284eae86d0b2
postgresql
PostgreSQL SQL dialect
5
2016-12-09T08:24:51
True
False
False
Feb 15, 2014 at 06:02 PM
/api/public/snipt/114036/
postgresql-active-connections-in-postgresql
<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="k">SELECT</span> <span class="n">t1</span><span class="mf">.</span><span class="o">*</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">FROM</span> <span class="n">PG_STAT_ACTIVITY</span> <span class="n">t1</span> </span><span id="L-3"><a name="L-3"></a><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="c1">-- En MySql sería: SHOW PROCESSSLIST</span> </span></pre></div> </td></tr></table>
postgresql

Bash

Run a PostgreSQL SQL script

psql -v ON_ERROR_STOP=1 -U <username> -d <database_name> -1 -f <sql_file>
https://snipt.net/embed/3180df5e2cee20c0e448a08bb4e8db23/
https://snipt.net/raw/3180df5e2cee20c0e448a08bb4e8db23/
3180df5e2cee20c0e448a08bb4e8db23
bash
Bash
1
2016-12-04T07:23:00
True
False
False
Feb 18, 2014 at 05:08 AM
/api/public/snipt/99549/
run-an-postgresql-sql-script
<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 -v <span class="nv">ON_ERROR_STOP</span><span class="o">=</span><span class="m">1</span> -U &lt;username&gt; -d &lt;database_name&gt; -1 -f &lt;sql_file&gt; </span></pre></div> </td></tr></table>
postgresql
Copyrighted, illegal, or inappropriate content? Email [email protected].