{"id":1499,"date":"2016-06-15T16:03:30","date_gmt":"2016-06-15T13:03:30","guid":{"rendered":"http:\/\/www.fatihacar.com\/blog\/?p=1499"},"modified":"2016-06-22T11:59:42","modified_gmt":"2016-06-22T08:59:42","slug":"postgresql-frequently-using-commands","status":"publish","type":"post","link":"http:\/\/www.fatihacar.com\/blog\/postgresql-frequently-using-commands\/","title":{"rendered":"PostgreSQL Frequently Using Commands"},"content":{"rendered":"<p><strong>List,\u00a0Add, Drop Database<\/strong><\/p>\n<blockquote><p><strong>List databases<\/strong><\/p>\n<p>postgres=# \\d<\/p>\n<p><strong>Add database<\/strong><\/p>\n<p>postgres=# createdb testdb<\/p>\n<p>or<\/p>\n<p>postgres=# create database testdb with owner facar;<\/p>\n<p><strong><br \/>\nDrop database<\/strong><\/p>\n<p>postgres=# dropdb testdb<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1512\" src=\"http:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo.png\" alt=\"Postgres_logo\" width=\"377\" height=\"137\" srcset=\"http:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo.png 377w, http:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo-300x109.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/p>\n<p><strong>List, Add, Drop, Grant User and Schema<\/strong><\/p>\n<blockquote><p><strong>List Users<\/strong><\/p>\n<p>postgres=# select * from pg_user;<\/p>\n<p><strong>Add User<\/strong><\/p>\n<p>postgres=# create user facar with encrypted password &#8216;password&#8217;;<\/p>\n<p>or<\/p>\n<p>postgres=# createuser (interactive user create)<\/p>\n<p><strong>Change User Password<\/strong><\/p>\n<p>postgres=# \\password facar<br \/>\npostgres=# alter user facar with encrypted password &#8216;newpassword&#8217;;<\/p>\n<p><strong>Create Schema<\/strong><\/p>\n<p>postgres=# create schema schfacar;<br \/>\npostgres=# grant usage on schema schfacar to facar;<br \/>\npostgres=# grant all on schema schfacar to facar;<\/p>\n<p><strong>Add Search Path<\/strong><\/p>\n<p>postgres=# alter user facar set search_path=schfacar;<\/p>\n<p><strong>Grant Permissions to User<\/strong><\/p>\n<p>postgres=# alter user facar superuser;<br \/>\npostgres=# alter user facar createdb;<br \/>\npostgres=# alter user facar replication;<br \/>\npostgres=# alter user facar bypassrls;<br \/>\npostgres=# grant all privileges on database testdb to facar;<\/p>\n<p><strong>Revoke Permissions from User<\/strong><\/p>\n<p>postgres=# alter user facar nosuperuser;<br \/>\npostgres=# alter user facar nocreatedb;<br \/>\npostgres=# alter user facar noreplication;<br \/>\npostgres=# alter user facar nobypassrls;<br \/>\npostgres=# revoke all privileges on database testdb from facar;\n<\/p><\/blockquote>\n<p><strong>Connect Database<\/strong><\/p>\n<blockquote><p>\n[root@testdb ~]\u00a0su &#8211; postgres<br \/>\n-bash-4.2$ psql<br \/>\n-bash-4.2$ psql testdb (connect testdb)<br \/>\n-bash-4.2$ psql -U facar testdb (connect testdb with facar user)<br \/>\n-bash-4.2$ psql -U facar testdb -h &#8220;192.168.20.12&#8221; -p 5432 (connect testdb with facar user on database that is 192.168.20.12 IP and 5432 port)<\/p>\n<p>[root@testdb ~]\u00a0 psql<br \/>\n[root@testdb ~]\u00a0 psql testdb (connect testdb)<br \/>\n[root@testdb ~]\u00a0 psql -U facar testdb (connect testdb with facar user)<br \/>\n[root@testdb ~]\u00a0 psql -U facar testdb -h &#8220;192.168.20.12&#8221; -p 5432 (connect testdb with facar user on database that is 192.168.20.12 IP and 5432 port)<\/p>\n<p><strong>Note :<\/strong> If you want to connect database with different user from postgres, you have to add line pg_hba.conf like that<\/p>\n<p>host \u00a0 \u00a0 \u00a0 \u00a0 all \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0all \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 192.168.20.12\/32 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0md5<\/p><\/blockquote>\n<p><strong>Information Queries<\/strong><\/p>\n<blockquote><p>postgres=# \\conninfo (current connection information)<br \/>\npostgres=# \\dt (tables of current user schema)<br \/>\npostgres=# \\d (list of object of current user schema)<br \/>\npostgres=# \\d tablename (columns of tablename)<br \/>\npostgres=# \\df (list of functions)<br \/>\npostgres=# \\df+ (list of functions with details)<br \/>\npostgres=# select version(); (detailed version of database)<br \/>\npostgres=# \\? (psql commands list for help)<br \/>\npostgres=# \\h (list of help options)<br \/>\npostgres=# \\h CREATE USER (help of CREATE USER command)<br \/>\npostgres=# \\timing (open timing to show query execution time)<br \/>\npostgres=# \\c testdb (connect testdb)<br \/>\npostgres=# \\c testdb facar (connect testdb with facar user)<br \/>\npostgres=# select pg_database_size(current_database()); (size of current database as byte)<br \/>\npostgres=# select pg_database_size(&#8216;testdb&#8217;); (size of testdb database as byte)<br \/>\npostgres=# select pg_relation_size(&#8216;tablename&#8217;); (size of table as byte)<br \/>\npostgres=# select usename, datname from pg_stat_activity; (who are connected to which database now)<br \/>\npostgres=# select pg_reload_conf(); (to reload configurations like service postgresql reload)<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>List,\u00a0Add, Drop Database List databases postgres=# \\d Add database postgres=# createdb testdb or postgres=# create database testdb with owner facar; Drop database postgres=# dropdb testdb List, Add, Drop, Grant User and Schema List Users postgres=# select * from pg_user; Add User postgres=# create user facar with encrypted password &#8216;password&#8217;; or postgres=# createuser (interactive user create)&#8230;<\/p>\n","protected":false},"author":37,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,7],"tags":[14,93,88],"class_list":["post-1499","post","type-post","status-publish","format-standard","hentry","category-administration-postgresql","category-sql-postgresql","tag-database-administration","tag-postgresql","tag-postgresql-administration"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p39NFI-ob","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/1499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/comments?post=1499"}],"version-history":[{"count":17,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/1499\/revisions"}],"predecessor-version":[{"id":1518,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/1499\/revisions\/1518"}],"wp:attachment":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media?parent=1499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/categories?post=1499"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/tags?post=1499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}