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

2. login the postgres

     psql -h hostname -U username

     psql -h localhost -U postgres

   Postgres Basic Commands for Beginners

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

 

3. list all the databases

     \l or \list

    Postgres Basic Commands for Beginners

  

4. list all the roles

     \du

    Postgres Basic Commands for Beginners

     

5. for help

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

     it depends on the context.

 

6. create a new database

     create database databasename

     create database mydb;

    Postgres Basic Commands for Beginners

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

    Postgres Basic Commands for Beginners

     

7. connect to the database

     \c databasename

    \c mydb

     or \connect mydb

    Postgres Basic Commands for Beginners 

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

9. list tables in  connected database

     \dt

    Postgres Basic Commands for Beginners

10. list the schema of a table

     \d tablename

     \d users

    Postgres Basic Commands for Beginners 

11. get current database name

     select current_database();

    Postgres Basic Commands for Beginners

……

 

 

     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

相关文章: