Postgres Basic Commands for Beginners

pengisgood發表於2013-07-31

  Just sharing what I have learned about postgres recently. Here is a part of basic commands you may need. Enjoy it!

  Before try the following commands, please ensure you are followed the introductions on github: https://github.com/thoughtworks/vagrant-postgresql.

 

1. boot up a database by virtualBox and vagrant

     script/database up

    

2. login the postgres

     psql -h hostname -U username

     psql -h localhost -U postgres

   

     Note: password: it depends. Maybe "postgres" or "password" or "secret".

 

3. list all the databases

     \l or \list

    

  

4. list all the roles

     \du

    

     

5. for help

     "\h" or "\?" or "help"

     it depends on the context.

 

6. create a new database

     create database databasename

     create database mydb;

    

     list all the databases, and you can find the database just created.

    

     

7. connect to the database

     \c databasename

    \c mydb

     or \connect mydb

     

8. create a table

     create table users (user_id bigserial primary key, user_name varchar(20) not null );

     Noteauto_increment is a MySQL feature. Postgres uses serial columns for the same purpose.

    

9. list tables in  connected database

     \dt

    

10. list the schema of a table

     \d tablename

     \d users

     

11. get current database name

     select current_database();

    

……

 

 

     Hopefully these commands will help you getting started with postgres command line quickly. For more information, please feel free to discuss with me or turn to these website:

     http://www.postgresql.org/docs/9.1/static/app-psql.html,

     http://www.commandprompt.com/ppbook/c4890

相關文章