snipt

Ctrl+h for KB shortcuts

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-09-25T07:14:05
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 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-09-25T08:42:31
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 id="L-1"><a name="L-1"></a><span class="nv">$ </span>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-09-21T15:35:34
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 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-09-22T10:30:59
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 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-09-21T16:16:46
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 id="L-1"><a name="L-1"></a><span class="c">#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="c">#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 support@snipt.net.