Createdb of PostgreSQL Client commands

Source: Internet
Author: User
Tags locale postgresql psql postgres database postgresql client

postgresql      Span style= "Font-size:12px;font-family:verdana, ' Sans-serif '; color: #0000FF;" >createdb [option ...] [dbname] [Description]      1. command-line Options list:

< Strong> option


-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.


[Email protected] pg_9.5_201510051]$ psql-u postgres-p 36985

Psql.bin (9.5.9)

Type ' help ' for help.


[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


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;


postgres=# \du List of roles

Role name | Attributes | Member of


MyUser | | {}

Postgres | Superuser, create role, create DB, Replication, Bypass RLS | {}


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

Createdb of PostgreSQL Client commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.