Your SlideShare is downloading. ×
15 practical postgre sql database administration commands
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

15 practical postgre sql database administration commands

1,691
views

Published on

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,691
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... Home About Free  eBook Archives Best  of  the  Blog Contact 15  Prac(cal  PostgreSQL  Database  Administra(on  Commands by  Ramesh  Natarajan  on  April  16,  2009 15 Like 7 Tweet 16 Earlier  we  discussed  about  how  to  install  PostgreSQL  database  on  Linux  from  source. In  this  arNcle,  let  us  review  top  15  pracNcal  postgreSQL  DBA  command  examples. If  you  are  a  mySQL  administrator,  check-­‐out  our  15  examples  of  mysqladmin  command  arNcle  that we  discussed  a  while  back. 1.  How  to  change  PostgreSQL  root  user  password  ? $ /usr/local/pgsql/bin/psql postgres postgres Password: (oldpassword) # ALTER USER postgres WITH PASSWORD tmppassword; $ /usr/local/pgsql/bin/psql postgres postgres Password: (tmppassword) Changing  the  password  for  a  normal  postgres  user  is  similar  as  changing  the  password  of  the  root  user.  Root  user  can  change  the  password  of  any  user,  and  the normal  users  can  only  change  their  passwords  as  Unix  way  of  doing. # ALTER USER username WITH PASSWORD tmppassword; 2.  How  to  setup  PostgreSQL  SysV  startup  script? $ su - root # tar xvfz postgresql-8.3.7.tar.gz # cd postgresql-8.3.7 # cp contrib/start-scripts/linux /etc/rc.d/init.d/postgresql # chmod a+x /etc/rc.d/init.d/postgresql 3.  How  to  check  whether  PostgreSQL  server  is  up  and  running? $ /etc/init.d/postgresql status Password: pg_ctl: server is running (PID: 6171) /usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" [Note: The status above indicates the server is up and running]1  of  7 18  Apr  12  7:18  pm
  • 2. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... $ /etc/init.d/postgresql status Password: pg_ctl: no server running [Note: The status above indicates the server is down] 4.  How  to  start,  stop  and  restart  PostgreSQL  database? # service postgresql stop Stopping PostgreSQL: server stopped ok # service postgresql start Starting PostgreSQL: ok # service postgresql restart Restarting PostgreSQL: server stopped ok 5.  How  do  I  find  out  what  version  of  PostgreSQL  I  am  running? $ /usr/local/pgsql/bin/psql test Welcome to psql 8.3.7, the PostgreSQL interactive terminal. Type: copyright for distribution terms h for help with SQL commands ? for help with psql commands g or terminate with semicolon to execute query q to quit test=# select version(); version ---------------------------------------------------------------------------------------------------- PostgreSQL 8.3.7 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42) (1 row) test=# 5.  How  to  create  a  PostgreSQL  user  ? There  are  two  methods  in  which  you  can  create  user. Method  1:  CreaNng  the  user  in  the  PSQL  prompt,  with  CREATE  USER  command. # CREATE USER ramesh WITH password tmppassword; CREATE ROLE Method  2:  CreaNng  the  user  in  the  shell  prompt,  with  createuser  command. $ /usr/local/pgsql/bin/createuser sathiya Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) n Shall the new role be allowed to create more new roles? (y/n) n CREATE ROLE 6.  How  to  create  a  PostgreSQL  Database  ? There  are  two  metods  in  which  you  can  create  two  databases. Method  1:  CreaNng  the  database  in  the  PSQL  prompt,  with  createuser  command. # CREATE DATABASE mydb WITH OWNER ramesh; CREATE DATABASE Method  2:  CreaNng  the  database  in  the  shell  prompt,  with  createdb  command. $ /usr/local/pgsql/bin/createdb mydb -O ramesh CREATE DATABASE *  -­‐O  owner  name  is  the  opNon  in  the  command  line.2  of  7 18  Apr  12  7:18  pm
  • 3. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... 7.  How  do  I  get  a  list  of  databases  in  a  Postgresql  database  ? # l [Note: This is backslash followed by lower-case L] List of databases Name | Owner | Encoding ----------+----------+---------- backup | postgres | UTF8 mydb | ramesh | UTF8 postgres | postgres | UTF8 template0 | postgres | UTF8 template1 | postgres | UTF8 8.  How  to  Delete/Drop  an  exis(ng  PostgreSQL  database  ? # l List of databases Name | Owner | Encoding ----------+----------+---------- backup | postgres | UTF8 mydb | ramesh | UTF8 postgres | postgres | UTF8 template0 | postgres | UTF8 template1 | postgres | UTF8 # DROP DATABASE mydb; DROP DATABASE 9.  GeWng  help  on  postgreSQL  commands ?  will  show  PSQL  command  prompt  help.  h  CREATE  will  shows  help  about  all  the  commands  that  starts  with  CREATE,  when  you  want  something  specific  such as  help  for  creaNng  index,  then  you  need  to  give  CREATE  INDEX. # ? # h CREATE # h CREATE INDEX 10.  How  do  I  get  a  list  of  all  the  tables  in  a  Postgresql  database? # d On  an  empty  database,  you’ll  get  “No  relaNons  found.”  message  for  the  above  command. 11.  How  to  turn  on  (ming,  and  checking  how  much  (me  a  query  takes  to  execute  ? #  Nming  —  Ager  this  if  you  execute  a  query  it  will  show  how  much  Nme  it  took  for  doing  it. # timing Timing is on. # SELECT * from pg_catalog.pg_attribute ; Time: 9.583 ms 12.  How  To  Backup  and  Restore  PostgreSQL  Database  and  Table? We  discussed  earlier  how  to  backup  and  restore  postgres  database  and  tables  using  pg_dump  and  psql  uNlity. 13.  How  to  see  the  list  of  available  func(ons  in  PostgreSQL  ? To  get  to  know  more  about  the  funcNons,  say  df+ # df # df+ 14.  How  to  edit  PostgreSQL  queries  in  your  favorite  editor  ? # e3  of  7 18  Apr  12  7:18  pm
  • 4. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... e  will  open  the  editor,  where  you  can  edit  the  queries  and  save  it.  By  doing  so  the  query  will  get  executed. 15.  Where  can  i  find  the  postgreSQL  history  file  ? Similar  to  the  Linux  ~/.bash_history  file,  postgreSQL  stores  all  the  sql  command  that  was  executed  in  a  history  filed  called  ~/.psql_history  as  shown  below. $ cat ~/.psql_history alter user postgres with password tmppassword; h alter user select version(); create user ramesh with password tmppassword; timing select * from pg_catalog.pg_attribute; 15 Tweet 16 Like 7  Share  Comment If  you  enjoyed  this  ar(cle,  you  might  also  like.. 1. 50  Linux  Sysadmin  Tutorials Awk  IntroducNon  –  7  Awk  Print  Examples 2. 50  Most  Frequently  Used  Linux  Commands  (With  Examples) Advanced  Sed  SubsNtuNon  Examples 3. Top  25  Best  Linux  Performance  Monitoring  and  Debugging  Tools 8  EssenNal  Vim  Editor  NavigaNon  Fundamentals 4. Mommy,  I  found  it!  –  15  PracNcal  Linux  Find  Command  Examples 25  Most  Frequently  Used  Linux  IPTables  Rules  Examples 5. Linux  101  Hacks  2nd  EdiNon  eBook   Turbocharge  PuTTY  with  12  Powerful  Add-­‐Ons Tags:  Change  Postgres  Password,  IdenNfy  PostgreSQL  Version,  Postgres,  PostgreSQL  database,  PostgreSQL  DB,  PostgreSQL  DBA  Commands,  PostgreSQL  psql command,  Start  and  Stop  PostgreSQL  Database,  ~/.psql_history {  6  comments…  read  them  below  or  add  one  } 1  sasikala  April  16,  2009  at  10:20  pm It  is  very  simple  but  more  useful.  thanks. 2  mike  March  21,  2010  at  10:41  pm very  useful  for  beginners  like  me.  Thanks. 3  karel  April  28,  2010  at  2:59  am ager  using d i  get  list  of  the  tables  ,and  highlighted (END) i  cannot  enter  any  text  or  command.  How  to  exit  the  descripNon  and  input  next  commands? 4  markitus82  May  18,  2010  at  10:03  am thanks!  nice  guide!4  of  7 18  Apr  12  7:18  pm
  • 5. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... 5  NR  August  11,  2010  at  8:54  pm Some  clarificaNons: d  gaves  you  more  then  just  tables,  to  see  only  tables  use  dt.  To  see  views  use  dv,  to  see  a  table  or  any  other  object’s  descripNon  use  d  objectname. You  can  use  ?  to  see  a  list  of  commands  and  ?  commandname  to  get  help  with  a  command.  The  same  way  you  can  use  h  to  see  a  list  of  SQL commands  and  h  sqlcommand  to  get  help  with  a  SQL  command. Be  aware  that  a  simple  create  database  command  will  create  a  database  with  default  parameters,  this  is  not  always  what  we  want  or  need.  Usually  it  is ok  for  english  speakers  but  other  languages  migth  need  a  different  collaNng  order  or  even  encoding,  as  the  default  postgresql  installaNon  does  not  (or did  not)  use  UTF-­‐8.  To  change  this  and  other  parameters  in  a  safe  and  permanent  way  you  migth  need  to  create  a  all  new  database  cluster  as  some parameters  cannot  be  changed  ager  database  creaNon  and  others  have  to  be  common  to  all  databases  (see  hTp://www.postgresql.org/docs/8.4 /interacNve/locale.html  for  postgreSQL  8.4). Has  for  the  Karel’s  problem,  simply  use  ‘q’  to  quit  the  viewing  and  came  back  to  the  prompt. 6  Anonymous  September  20,  2011  at  11:50  pm this  is  very  useful  for  me Leave  a  Comment Name E-­‐mail Website  NoNfy  me  of  followup  comments  via  e-­‐mail Submit Previous  post:  Open  &  View  10  Different  File  Types  with  Linux  Less  Command  –  The  UlNmate  Power  of  Less Next  post:  Ctags  and  Taglist:  Convert  Vim  Editor  to  BeauNful  Source  Code  Browser  for  Any  Programming  Language Sign  up  for  our  free  email  newsleTer   you@address.com           Sign Up            RSS    TwiTer    Facebook   Search EBOOKS5  of  7 18  Apr  12  7:18  pm
  • 6. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... POPULAR  POSTS 12  Amazing  and  EssenNal  Linux  Books  To  Enrich  Your  Brain  and  Library 50  UNIX  /  Linux  Sysadmin  Tutorials 50  Most  Frequently  Used  UNIX  /  Linux  Commands  (With  Examples) How  To  Be  ProducNve  and  Get  Things  Done  Using  GTD 30  Things  To  Do  When  you  are  Bored  and  have  a  Computer Linux  Directory  Structure  (File  System  Structure)  Explained  with  Examples Linux  Crontab:  15  Awesome  Cron  Job  Examples Get  a  Grip  on  the  Grep!  –  15  PracNcal  Grep  Command  Examples Unix  LS  Command:  15  PracNcal  Examples 15  Examples  To  Master  Linux  Command  Line  History Top  10  Open  Source  Bug  Tracking  System Vi  and  Vim  Macro  Tutorial:  How  To  Record  and  Play Mommy,  I  found  it!  -­‐-­‐  15  PracNcal  Linux  Find  Command  Examples 15  Awesome  Gmail  Tips  and  Tricks 15  Awesome  Google  Search  Tips  and  Tricks RAID  0,  RAID  1,  RAID  5,  RAID  10  Explained  with  Diagrams Can  You  Top  This?  15  PracNcal  Linux  Top  Command  Examples Top  5  Best  System  Monitoring  Tools Top  5  Best  Linux  OS  DistribuNons How  To  Monitor  Remote  Linux  Host  using  Nagios  3.0 Awk  IntroducNon  Tutorial  –  7  Awk  Print  Examples How  to  Backup  Linux?  15  rsync  Command  Examples The  UlNmate  Wget  Download  Guide  With  15  Awesome  Examples Top  5  Best  Linux  Text  Editors Packet  Analyzer:  15  TCPDUMP  Command  Examples The  UlNmate  Bash  Array  Tutorial  with  15  Examples6  of  7 18  Apr  12  7:18  pm
  • 7. 15  PracNcal  PostgreSQL  Database  AdministraNon  Commands hTp://www.thegeekstuff.com/2009/04/15-­‐pracNcal-­‐postgresql... 3  Steps  to  Perform  SSH  Login  Without  Password  Using  ssh-­‐keygen  &  ssh-­‐copy-­‐id Unix  Sed  Tutorial:  Advanced  Sed  SubsNtuNon  Examples UNIX  /  Linux:  10  Netstat  Command  Examples The  UlNmate  Guide  for  CreaNng  Strong  Passwords 6  Steps  to  Secure  Your  Home  Wireless  Network Turbocharge  PuTTY  with  12  Powerful  Add-­‐Ons About  The  Geek  Stuff  My  name  is  Ramesh  Natarajan.  I  will  be  posNng  instrucNon  guides,  how-­‐to,  troubleshooNng  Nps  and  tricks  on  Linux,  database, hardware,  security  and  web.  My  focus  is  to  write  arNcles  that  will  either  teach  you  or  help  you  resolve  a  problem.  Read  more  about  Ramesh  Natarajan and  the  blog. Support  Us Support  this  blog  by  purchasing  one  of  my  ebooks. Bash  101  Hacks  eBook Sed  and  Awk  101  Hacks  eBook Vim  101  Hacks  eBook Nagios  Core  3  eBook Contact  Us Email  Me  :  Use  this  Contact  Form  to  get  in  touch  me  with  your  comments,  quesNons  or  suggesNons  about  this  site.  You  can  also  simply  drop  me  a  line to  say  hello!. Follow  us  on  TwiTer Become  a  fan  on  Facebook     Copyright  ©  2008–2012  Ramesh  Natarajan.  All  rights  reserved  |  Terms  of  Service  |  AdverNse7  of  7 18  Apr  12  7:18  pm

×