{"id":489,"date":"2014-09-08T16:18:45","date_gmt":"2014-09-08T16:18:45","guid":{"rendered":"http:\/\/shirishranjit.com\/blog1\/?page_id=489"},"modified":"2016-03-20T22:48:44","modified_gmt":"2016-03-21T02:48:44","slug":"postgres-database-cheatsheet","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/technical-posts\/postgres-database-cheatsheet","title":{"rendered":"Postgres Database Setup\/Upgrade and Start\/Stop Cheatsheet"},"content":{"rendered":"<h2>starting<\/h2>\n<pre>\r\n\/usr\/pgsql-9.4\/bin\/pg_ctl -D \/var\/lib\/pgsql\/9.4\/data\/ -l logfile start\r\n<\/pre>\n<h2>stopping<\/h2>\n<pre>\r\n\/usr\/pgsql-9.4\/bin\/pg_ctl stop -m fast\r\n<\/pre>\n<h2>Logging into postgres<\/h2>\n<pre><code>sudo -i -u postgres psql\r\n<\/code><\/pre>\n<h2>Resetting Password for postgres <\/h2>\n<ol>\n<li>First Change the &#8220;pg_hba.conf&#8221; file to do all trust method<\/li>\n<li>Stop the database server<\/li>\n<li>Start the database server<\/li>\n<li>Login as below:\n<pre>\r\npsql -U postgres -W  -h localhost\r\n<\/pre>\n<\/li>\n<li>If you are asked for password, just type &#8220;Enter&#8221; &#8211; Now you are logged in.<\/li>\n<li>Change Password as follows:\n<pre> ALTER ROLE postgres WITH PASSWORD \u2018?????\u2019; <\/pre>\n<\/li>\n<li>Now change back your &#8220;pg_hba.conf&#8221; file to the original so that the password can be applied<\/li>\n<li>Stop the database<\/li>\n<li>Start the database<\/li>\n<p>Now use your new password and it should work<\/p>\n<\/ol>\n<h2> Creating a new User in database <\/h2>\n<p>Execute following command<\/p>\n<pre>createuser --interactive \"username\" <\/pre>\n<p>This will give options for follow: <\/p>\n<pre>\r\nShall the new role be a superuser? (y\/n) n\r\nShall the new role be allowed to create databases? (y\/n) y\r\nShall the new role be allowed to create more new roles? (y\/n) y\r\n<\/pre>\n<p>Selecting Activity in database<\/p>\n<div class=\"code panel pdl\">\n<div class=\"codeContent panelContent pdl\">\n<div>\n<div id=\"highlighter_512630\" class=\"syntaxhighlighter nogutter  sql\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\" title=\"Hint: double-click to select code\">\n<div class=\"line number1 index0 alt2\"><code class=\"sql keyword\">select<\/code> <code class=\"sql plain\">pid, usename, query_start, state, query <\/code><code class=\"sql keyword\">from<\/code> <code class=\"sql plain\">pg_stat_activity;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2>Upgrading the Postgresql to 9.5 version<\/h2>\n<pre>\r\n<code>\r\nwget https:\/\/alioth.debian.org\/scm\/loggerhead\/pkg-postgresql\/postgresql-common\/trunk\/download\/head:\/apt.postgresql.org.s-20130224224205-px3qyst90b3xp8zj-1\/apt.postgresql.org.sh\r\nchmod a+x apt.postgresql.org.sh\r\nsed -i -e 's\/$CODENAME-pgdg main\/$CODENAME-pgdg main 9.5\/g' apt.postgresql.org.sh\r\nsudo .\/apt.postgresql.org.sh\r\n\r\nsudo apt-get install postgresql-9.5\r\nsudo apt-get install postgresql-client-9.5\r\nsudo apt-get install postgresql-contrib-9.5\r\n\r\n<\/code>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Canceling a query in db<\/p>\n<div class=\"code panel pdl\">\n<div class=\"codeContent panelContent pdl\">\n<div>\n<div id=\"highlighter_288165\" class=\"syntaxhighlighter nogutter  sql\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\" title=\"Hint: double-click to select code\">\n<div class=\"line number1 index0 alt2\"><code class=\"sql keyword\">select<\/code> <code class=\"sql plain\">pg_cancel_backend(pid)<\/code><code class=\"sql keyword\">select<\/code> <code class=\"sql plain\">pg_terminate_backend(pid)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"PostgresDatabaseNotes-ExtractdataasCVS:\">Extract data as CVS:<\/h2>\n<p>copying reports table.<\/p>\n<div class=\"code panel pdl\">\n<div class=\"codeContent panelContent pdl\">\n<div>\n<div id=\"highlighter_731395\" class=\"syntaxhighlighter nogutter  sql\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\" title=\"Hint: double-click to select code\">\n<div class=\"line number1 index0 alt2\"><code class=\"sql plain\">\\copy reports <\/code><code class=\"sql keyword\">TO<\/code> <code class=\"sql string\">'reports_data_652014.csv'<\/code> <code class=\"sql plain\">CSV header<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<pre>\r\nSELECT rename, 100 * idx_scan \/ (seq_scan + idx_scan) percent_of_times_index_used, n_live_tup rows_in_table\r\nFROM pg_stat_user_tables\r\nWHERE seq_scan + idx_scan &gt; 0\r\nORDER BY n_live_tup DESC;\r\n<\/pre>\n<\/div>\n<h2>postgresql 9.5 psql peer authentication failed<\/h2>\n<p>he problem is still your pg_hba.conf file (\/etc\/postgresql\/9.1\/main\/pg_hba.conf). This line:<\/p>\n<p>local   all             postgres                                peer<br \/>\nShould be<\/p>\n<p>local   all             postgres                                trust<\/p>\n<h2>Helpful Links:<\/h2>\n<ol>\n<ol>\n<li><strong>Understanding How PostgreSQL Executes a Query<\/strong><\/li>\n<\/ol>\n<\/ol>\n<p><a title=\"http:\/\/etutorials.org\/SQL\/Postgresql\/Part+I+General+PostgreSQL+Use\/Chapter+4.+Performance\/Understanding+How+PostgreSQL+Executes+a+Query\/\" href=\"http:\/\/etutorials.org\/SQL\/Postgresql\/Part+I+General+PostgreSQL+Use\/Chapter+4.+Performance\/Understanding+How+PostgreSQL+Executes+a+Query\/\">http:\/\/etutorials.org\/SQL\/Postgresql\/Part+I+General+PostgreSQL+Use\/Chapter+4.+Performance\/Understanding+How+PostgreSQL+Executes+a+Query\/<\/a><\/p>\n<ol>\n<ol>\n<li><strong>PostgreSQL Hardware Performance Tuning<\/strong><\/li>\n<\/ol>\n<\/ol>\n<p>#\u00a0<a title=\"http:\/\/momjian.us\/main\/writings\/pgsql\/hw_performance\/\" href=\"http:\/\/momjian.us\/main\/writings\/pgsql\/hw_performance\/\">http:\/\/momjian.us\/main\/writings\/pgsql\/hw_performance\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/docs\/9.1\/static\/server-start.html\">http:\/\/www.postgresql.org\/docs\/9.1\/static\/server-start.html<\/a><\/p>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Postgres+Database+Setup%2FUpgrade+and+Start%2FStop+Cheatsheet\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/technical-posts\/postgres-database-cheatsheet\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>starting \/usr\/pgsql-9.4\/bin\/pg_ctl -D \/var\/lib\/pgsql\/9.4\/data\/ -l logfile start stopping \/usr\/pgsql-9.4\/bin\/pg_ctl stop -m fast Logging into postgres sudo -i -u postgres psql Resetting Password for postgres First Change the &#8220;pg_hba.conf&#8221; file to do all trust method Stop the database server Start the &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/technical-posts\/postgres-database-cheatsheet\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":198,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-489","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/489"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":23,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/489\/revisions"}],"predecessor-version":[{"id":1236,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/489\/revisions\/1236"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/198"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}