Saturday, January 15, 2011

PostgreSQL Commands



Reference : http://www.linuxweblog.com/blogs/sandip/20101203/listing-directories-tree-format


Create db user and grant permission to create databases.
# adduser <dbuser>
# su - postgres
$ createuser -d -S -R <dbuser>



simple commands for administering the database. Hopefully these notes will help as reference when working with PostgreSQL:
  1. Login as "postgres" (SuperUser) to start using database:
    # su - postgres
  2. Create a new database:
    $ createdb mydb
  3. Drop database:
    $ dropdb mydb
  4. Access database:
    $ psql mydb
  5. Get help:
    mydb=# \h
  6. Quit:
    mydb=# \q
  7. Read command from file:
    mydb=# \i input.sql
  8. To dump a database:
    $ pg_dump mydb > db.out
  9. To reload the database:
    $ psql -d database -f db.out
  10. Dump all database:
    # su - postgres
    # pg_dumpall > /var/lib/pgsql/backups/dumpall.sql
  11. Restore database:
    # su - postgres
    # psql -f /var/lib/pgsql/backups/dumpall.sql mydb
  12. Show databases:
    #psql -l
    or
    mydb=# \l;
  13. Show users:
    mydb=# SELECT * FROM "pg_user";
  14. Show tables:
    mydb=# SELECT * FROM "pg_tables";
  15. Set password:
    mydb=# UPDATE pg_shadow SET passwd = 'new_password' where usename = 'username';
  16. Clean all databases (Should be done via a daily cron):
    $ vacuumdb --quiet --all
    DROP TABLE name [, ...] [ CASCADE ]
    DROP TABLE removes tables from the database. Only its owner may destroy a table. To empty a table of rows, without destroying the table, use DELETE.
    DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table. However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified. CASCADE will remove a dependent view entirely, but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely.




    Add the below line to /var/lib/pgsql/data/postgresql.conf to make postgresql database listen to external connections:
    listen_addresses = '*'
    Edit /var/lib/pgsql/data/pg_hba.conf and add the appropriate permissions:
    host all all 0.0.0.0/0 md5
     
     
     
     

    change postgresql database owner

    template1=# ALTER DATABASE <dbname> OWNER TO <dbuser>;
     
     
     
     
     
     

    setup postgresql database to use passwords to connect

    Edit "/var/lib/pgsql/data/pg_hba.conf" with:
    local   all       all                          md5
    host    all       all       127.0.0.1/32       md5
    local - socket connection
    host - tcp connection
    To connect via socket:

    $ psql -U <username> -d <dbname>
    To connect via tcp:

    $ psql -U <username> -h <hostname> -d <dbname>
     
     
     
     
     

    password reset

    template1=# ALTER USER <dbuser> WITH PASSWORD '<password>';

    Viewing owner and permissions

    \d -- view the owner
    \dp -- view permissions
     
     
     
     

1 comment:

  1. Thanks for giving the useful command listing.I am a great fan of Postgresql.Its a great DBMS and on top of that its available free.I used it with Adempier.
    Lauren
    sap upgrade planning

    ReplyDelete