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.