postgresql Span style= "Font-size:12px;font-family:verdana, ' Sans-serif '; color: #0000FF;" >createdb [option ...] [dbname] [Description] 1. command-line Options list:
< Strong> option |
Description |
-d (--tablespace=tablespace) |
Specifies the default table space for the database. |
-e (--echo) |
echo createdb The generated command and sends it to the server. |
-e ( --encoding=encoding ) |
Specifies how character encoding is used for this database. |
-l ( --locale=locale ) |
Specifies the localization settings used for this database. |
-o ( --owner=owner ) |
Specify the owner of the new database, If this option is not specified, the value is the currently logged on user. |
-t ( --template=template ) |
Specifies the template database to create this database. |
-h ( --host=host ) |
specify postgresql host name of the server. |
-p (--port=port) |
Specifies the server's listening port, if not specified, is the default 5432 |
-u (--username= username) |
The logon user name for this operation, if -o " option is not specified, this database is owner will be the logged-on user. |
-w ( --no-password ) |
If the current logged-on user does not have a password, You can specify this option to log in directly. |
2. Application Example:
#1. log in as postgres .
Login to the default Postgres database (three ways to log in)
[Email protected] pg_9.5_201510051]$ psql-p 36985
Psql.bin (9.5.9)
Type ' help ' for help.
postgres=#
[Email protected] pg_9.5_201510051]$ psql-u postgres-p 36985
Psql.bin (9.5.9)
Type ' help ' for help.
postgres=#
[Email protected] pg_9.5_201510051]$ psql-u postgres-d postgres-p 36985
Psql.bin (9.5.9)
Type ' help ' for help.
postgres=# \q
#2. Create a table space.
postgres=# CREATE tablespace tbspace01 location '/data/postgresql/tbspace ';
CREATE tablespace
[Email protected] tbspace]$ Cd/data/postgresql/tbspace
[[email protected] tbspace]$ ls
pg_9.5_201510051
3. Create a new database owner .
postgres=# CREATE ROLE testwjw LOGIN PASSWORD ' 123456 ';
create ROLE
Span style= "Font-size:12px;font-family:verdana, ' Sans-serif '; color: #0000FF;" > postgres=# \q
#4. Create a new database where the logged-on user for this connection is Span style= "Font-family:verdana, Sans-serif;font-size:12px;color: #008000;" >TESTWJW owner new database named cstest01
[Email protected] ~]$ createdb-u testwjw-p36985-o testwjw-e cstest01
CREATE DATABASE cstest01 OWNER testwjw tablespace db_space01;
Createdb:database creation Failed:ERROR:permission denied to create database
The reason is that user TESTWJW does not have permission to create the library:
postgres=# alter user TESTWJW createdb;
ALTER ROLE
postgres=# \du List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
MyUser | | {}
Postgres | Superuser, create role, create DB, Replication, Bypass RLS | {}
TESTWJW | Create DB
5. Log back in to see if the database was created successfully by querying the system table and whether the tablespace and owner are consistent.
postgres=# SELECT datname,rolname,spcname from Pg_database db, pg_authid au, pg_tablespace ts WHERE datname = ' cstest01 ' and DATDBA = au.oid and dattablespace = ts.oid;
Datname | Rolname | Spcname
----------+---------+------------
cstest01 | TESTWJW | Pg_default
(1 row)
This article is from the "10931853" blog, please be sure to keep this source http://wujianwei.blog.51cto.com/10931853/1970757
Createdb of PostgreSQL Client commands