Title:
PostgreSQL
|
edited by
Youngrok Pak
at
10 years, 1 month ago.
<h2>자주 까먹는 명령어</h2>
<p>사용자 생성</p>
<pre>create role <username> with login password <password>;</pre>
<p>권한 지정</p>
<pre>grant usage on schema public to <username>;<br>grant all on schema public to <username>;<br>grant all privileges on database <dbname> to <username>;<br>grant all on all tables in schema public to <username><br>grant select on <table> to <username>;</pre>
<p>읽기 전용 계정 만들기 <a href="http://stackoverflow.com/questions/760210/how-do-you-create-a-read-only-user-in-postgresql">http://stackoverflow.com/questions/760210/how-do-you-create-a-read-only-user-in-postgresql</a></p>
<pre><code><span class="kwd">GRANT</span><span class="kwd">CONNECT</span><span class="kwd">ON</span><span class="kwd">DATABASE</span><span class="pln"> mydb </span><span class="kwd">TO</span><span class="pln"> xxx</span><span class="pun">;</span><span class="pln"><br></span></code><code><span class="kwd">GRANT</span><span class="pln"> USAGE </span><span class="kwd">ON</span><span class="kwd">SCHEMA</span><span class="kwd">public</span><span class="kwd">TO</span><span class="pln"> xxx</span><span class="pun">;</span></code><br><code><span class="pln"><code><span class="kwd">GRANT</span><span class="kwd">SELECT</span><span class="kwd">ON</span><span class="pln"> mytable </span><span class="kwd">TO</span><span class="pln"> xxx</span><span class="pun">;</span></code><br></span></code><code><span class="kwd">GRANT</span><span class="kwd">SELECT</span><span class="kwd">ON</span><span class="kwd">ALL</span><span class="pln"> TABLES </span><span class="kwd">IN</span><span class="kwd">SCHEMA</span><span class="kwd">public</span><span class="kwd">TO</span><span class="pln"> xxx</span><span class="pun">;</span></code></pre>
<p>커넥션 현황 보기</p>
<pre>SELECT count(*) from pg_stat_activity;</pre>
<h2><a href="파이썬">파이썬</a>에서 사용하기</h2>
<pre>import psycopg2<br>conn = psycopg2.connect('host=db.com user=youngrok password=blahblah dbname=mydb<br>cursor = conn.cursor()<br>cursor.execute('SELECT * FROM auth_user')<br>print cursor.fetchall()</pre>
<p>