Postgres Basic Commands for Beginners

  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

    Postgres Basic Commands for Beginners_第1张图片

2. login the postgres

     psql -h hostname -U username

     psql -h localhost -U postgres

   Postgres Basic Commands for Beginners_第2张图片

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

 

3. list all the databases

     \l or \list

    Postgres Basic Commands for Beginners_第3张图片

  

4. list all the roles

     \du

    Postgres Basic Commands for Beginners_第4张图片

     

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.

    Postgres Basic Commands for Beginners_第5张图片

     

7. connect to the database

     \c databasename

    \c mydb

     or \connect mydb

    Postgres Basic Commands for Beginners_第6张图片 

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.

    Postgres Basic Commands for Beginners_第7张图片

9. list tables in  connected database

     \dt

    Postgres Basic Commands for Beginners_第8张图片

10. list the schema of a table

     \d tablename

     \d users

    Postgres Basic Commands for Beginners_第9张图片 

11. get current database name

     select current_database();

    Postgres Basic Commands for Beginners_第10张图片

……

 

 

     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

你可能感兴趣的:(postgres)