码迷,mamicode.com
首页 > 其他好文 > 详细

Postgres study reminder ,lesson 1

时间:2015-09-19 01:06:25      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

Just want to learn the postgres database in details from now on, try to write this blog to push myself working on it.

This is the first lesson of my series for postgres,

Subject is very simple, prepare the postgres ENV , and try to create a new database and user.

I am going to use the docker for the ENV. Just try to use a new way, and try to make docker study useful here.

If you don‘t like it, or prefer the more tradional solution, Just check the official postgres installation tutorial.

If you like docker,but don‘t know how to start it, please try search for docker‘s offical doc. I just a freshman here. And I just want to keep focus on postgres here too.


Key codes for pg related issus are listed below:


#start my postgres db

docker run --name pgstudy -e POSTGRES_PASSWORD=1q -d postgres

#connect with the pgsql

docker run -it --link pgstudy:postgres --rm postgres sh -c ‘exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres‘


As the comments say,I just first create a new postgres container for server, named pgstudy,

then ,I use a oneshot psql client to working on it.


The result of the second command is listed below. 

[postgresqlCookbook]$ docker run -it --link pgstudy:postgres --rm postgres sh -c ‘exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres‘

Password for user postgres:

psql (9.4.4)

Type "help" for help.


postgres=#


As you can see, I can do the psql command now. HaHa.


Now, let‘s just first create the user , e.g. hr, password is hr too.

CREATE USER hr with PASSWORD ‘hr‘;

Now to create the db.(just copy the book material here):

PostgreSQL provides two methods to create a new database:

  • The first method relies on using the CREATE DATABASE SQL statement:

    CREATE DATABASE hrdb WITH ENCODING=‘UTF8‘ OWNER=hr CONNECTION LIMIT=25;
  • The second method requires using the createdb command-line executable:

    createdb –h localhost –p 5432 –U postgres testdb1

Now we can check for the database for confirmation:Just type "\l" in the pg command line:


postgres=# \l

                                 List of databases

   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges

-----------+----------+----------+------------+------------+-----------------------

 hrdb      | hr       | UTF8     | en_US.utf8 | en_US.utf8 |

 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |

 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

(4 rows)


postgres=#


OK . This is the beginning of my pg study  series. Hope it can carry on with a complete ending.

Postgres study reminder ,lesson 1

标签:

原文地址:http://my.oschina.net/hunterXue/blog/508286

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!