PostgreSQL tutorial of xvii: client command (1)

  • 2020-05-06 11:53:16
  • OfStack

zero, password file:

Before giving the other PostgreSQL client commands, we need to introduce the password file in PostgreSQL. This file is explained ahead of time here because we will use it heavily in the following sample code to ensure that our script can be automated. In other words, if the file is not provided during the execution of the client command, all client commands in PostgreSQL are interrupted by the password input prompt.
      in the current user's HOME directory, we need to manually create a password file with the file name.pgpass, so that when we connect to the PostgreSQL server, the client command will automatically read the file to get the password information needed to log in. The format of the file is as follows:
 


    hostname:port:database:username:password
 

The above data of       is separated by a colon, which is divided into five fields, which respectively represent the server host name (IP), the port number that the server listens to, the database name that the server accesses, the login user name and the password. See the following example:
 

    /> cat > .pgpass
    *:5432:postgres:postgres:123456
    CTRL+D
    #.pgpass The permission of the file must be 0600 To prevent access by any global or group of users, otherwise the file will be ignored.
    /> chmod 0600 .pgpass
 

Before learning the client commands that follow, we need to manually create the file for our application so that all the sample code that follows will use the password file so that it can be automated in a batch manner.

1, createdb:

      create a new PostgreSQL database. This command is used as follows:
 


    createdb [option...] [dbname] [description]
 

1. List of command line options:

选项 说明
-D(--tablespace=tablespace) 指定数据库的缺省表空间。
-e(--echo) 回显createdb生成的命令并且把它发送到服务器。
-E(--encoding=encoding) 指定用于此数据库的字符编码方式。
-l(--locale=locale) 指定用于此数据库的本地化设置。
-O(--owner=owner) 指定新建数据库的拥有者,如果未指定此选项,该值为当前登录的用户。
-T(--template=template) 指定创建此数据库的模板数据库。
-h(--host=host) 指定PostgreSQL服务器的主机名。
-p(--port=port) 指定服务器的侦听端口,如不指定,则为缺省的5432。
-U(--username=username) 本次操作的登录用户名,如果-O选项没有指定,此数据库的Owner将为该登录用户。
-w(--no-password) 如果当前登录用户没有密码,可以指定该选项直接登录。

2. Application example:


    #1. In order to postgres Login. ( Refer to the above password file for details )   
    /> psql
    #2. Create a table space.
    postgres=# CREATE TABLESPACE my_tablespace LOCATION '/opt/PostgreSQL/9.1/mydata';
    CREATE TABLESPACE
    #3. For creating a new database owner .
    postgres=# CREATE ROLE myuser LOGIN PASSWORD '123456';
    CREATE ROLE
    postgres=# \q
    #4. Create a new database where the logged-in user for this connection is postgres For the new database owner for myuser , the table space is my_tablespace , the new database name mydatabase .
    /> createdb -U postgres -O myuser -D my_tablespace -e mydatabase
    CREATE DATABASE mydatabase OWNER myuser TABLESPACE my_tablespace;
    #5. Log back in and query the system tables to see if the database was created successfully and if the table space and owner are consistent.
    /> psql
    postgres=# SELECT datname,rolname,spcname FROM pg_database db, pg_authid au, pg_tablespace ts WHERE datname = 'mydatabase' AND datdba = au.oid AND dattablespace = ts.oid;
      datname   | rolname |    spcname
    ------------+---------+---------------
     mydatabase | myuser  | my_tablespace
    (1 row)

:

      deletes an existing PostgreSQL database.
 


    dropdb [option...] dbname
 

      1. List of command line options:

 

选项 说明
-e(--echo) 回显dropdb生成的命令并且把它发送到服务器。
-i(--interactive) 在做任何破坏性动作前提示。
-q(--quiet) 不显示响应。
-h(--host=host) 指定PostgreSQL服务器的主机名。
-p(--port=port) 指定服务器的监听端口,如不指定,则为缺省的5432。
-U(--username=username) 本次操作的登录用户名。
-w(--no-password) 如果当前登录用户没有密码,可以指定该选项直接登录。

2. Application example:


    # In order to postgres The identity of the connection server, delete mydatabase The database.
    /> dropdb -U postgres -e mydatabase
    DROP DATABASE mydatabase;
    # Verify that the database has been deleted by looking at the system tables.
    /> psql
    postgres=# SELECT count(*) FROM pg_database WHERE datname = 'mydatabase';
     count
    -------
         0
    (1 row)

3, reindexdb:

      rebuilds the index for a specified PostgreSQL database.
 


    reindexdb [connection-option...] [--table | -t table ] [--index | -i index ] [dbname]
    reindexdb [connection-option...] [--all | -a]
    reindexdb [connection-option...] [--system | -s] [dbname]
 

      1. List of command line options:

 

选项 说明
-a(-all) 重建整个数据库的索引。
-e(--echo) 回显reindexdb生成的命令并且把它发送到服务器。
-i(--index=index) 仅重建指定的索引。
-q(--quiet) 不显示响应。
-s(--system) 重建数据库系统表的索引。
-t(--table=table) 仅重建指定数据表的索引。
-h(--host=host) 指定PostgreSQL服务器的主机名。
-p(--port=port) 指定服务器的监听端口,如不指定,则为缺省的5432。
-U(--username=username) 本次操作的登录用户名。
-w(--no-password) 如果当前登录用户没有密码,可以指定该选项直接登录。

  2. Example of application:  
 


    # Only rebuild the data table testtable All indexes on.
    /> reindexdb -t testtable -e -U postgres postgres
    REINDEX TABLE testtable;
    # Only the specified index is rebuilt testtable_idx
    /> reindexdb -i testtable_idx -e -U postgres postgres
    REINDEX INDEX testtable_idx;   
    # Rebuilding the specified database mydatabase The full index of.
    /> reindexdb mydatabase

4, vacuumdb:

      collects garbage and analyzes an PostgreSQL database.
 


    vacuumdb [-options] [--full | -f] [--verbose | -v] [--analyze | -z] [-t table [(column [,...])]] [dbname]
    vacuumdb [-options] [--all | -a] [--full | -f] [--verbose | -v] [--analyze | -z]
 

      1. List of command line options:
 
选项 说明
-a(--all) 清理所有数据库。
-e(--echo) 回显vacuumdb生成的命令并且把它发送到服务器。
-f(--full) 执行完全清理。
-q(--quiet) 不显示响应。
-t table [(column[,...])] 仅仅清理或分析指定的数据表,字段名只是在与--analyze选项联合使用时才需要声明。
-v(--verbose) 在处理过程中打印详细信息。
-z(--analyze) 计算用于规划器的统计值。
-h(--host=host) 指定PostgreSQL服务器的主机名。
-p(--port=port) 指定服务器的监听端口,如不指定,则为缺省的5432。
-U(--username=username) 本次操作的登录用户名。
-w(--no-password) 如果当前登录用户没有密码,可以指定该选项直接登录。

2. Application example:


    # Clean up the entire database mydatabase .  
    /> vacuumdb -e mydatabase
    VACUUM;
    # Clean up and analyze postgres In the database testtable Table.
    /> vacuumdb -e --analyze --table 'testtable' postgres
    VACUUM ANALYZE testtable;
    # Clean up and analyze postgres In the database testtable The table i Field.
    /> vacuumdb -e --analyze -t 'testtable(i)' postgres
    VACUUM ANALYZE testtable(i);
 

, createuser:

      defines a new PostgreSQL user account. It is important to note that only superusers or users with CREATEROLE privileges can execute this command. If you want to create a superuser, you can only execute the command as a superuser; in other words, a normal user with CREATEROLE privileges cannot create a superuser. This command is used as follows:
 


    createuser [option...] [username]
 

      1. List of command line options:

选项 说明
-c number 设置新创建用户的最大连接数,缺省为没有限制。
-d(--createdb) 允许该新建用户创建数据库。
-D(--no-createdb) 禁止该新建用户创建数据库。
-e(--echo) 回显createuser生成的命令并且把它发送到服务器。
-E(--encrypted) 对保存在数据库里的用户口令加密。如果没有声明, 则使用缺省值。
-i(--inherit) 新创建的角色将自动继承它的组角色的权限。
-I(--no-inherit) 新创建的角色不会自动继承它的组角色的权限。
-l(--login) 新角色将被授予登录权限,该选项为缺省选项。
-L(--no-login) 新角色没有被授予登录权限。
-N(--unencrypted) 不对保存在数据库里的用户口令加密。如果没有声明, 则使用缺省值。
-P(--pwprompt) 如果给出该选项,在创建用户时将提示设置口令。
-r(--createrole) 新角色被授予创建数据库的权限。
-R(--no-createrole) 新角色没有被授予创建数据库的权限。
-s(--superuser) 新角色为超级用户。
-S(--no-superuser) 新角色不是超级用户。
-h(--host=host) 指定PostgreSQL服务器的主机名。
-p(--port=port) 指定服务器的监听端口,如不指定,则为缺省的5432。
-U(--username=username) 本次操作的登录用户名。
-w(--no-password) 如果当前登录用户没有密码,可以指定该选项直接登录。

2. Application example:


    # For some options without default Settings, such as -(d/D) , -(s/S) and -(r/R) , if not specified directly on the command line, a prompt will be given on execution of the command.
    # It is important to note that the prompt will suspend the automation script and the command will not continue until it is typed.
    /> createuser -U postgres myuser
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) y
    Shall the new role be allowed to create more new roles? (y/n) n
    CREATE ROLE myuser NOSUPERUSER CREATEDB NOCREATEROLE INHERIT LOGIN;
    # through psql Once logged in, view the system view to verify that the user was successfully created and that the new role has the correct permissions.
    /> psql
    postgres=# SELECT rolname,rolsuper,rolinherit,rolcreaterole,rolcreatedb,rolcanlogin FROM pg_roles WHERE rolname = 'myuser';
     rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin
    ---------+----------+------------+---------------+-------------+-------------
     myuser  | f           | t             | f                  | t                 | t
    (1 row)
    # To ensure that the automation script is not suspended by the command's prompt, we need to specify all options without default values when the command is executed.
    /> createuser -U postgres -e -S -D -R myuser2
    CREATE ROLE myuser2 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;
    # We can immediately specify the user's password when the user is created -P Option complete, however, such usage is bound to suspend the automation script,
    # So we can compromise by not specifying a password when the user is created, and then manually writing that user's password after the automated script is successful.
    /> createuser -P -s -e myuser3
    Enter password for new role:
    Enter it again:
    CREATE ROLE myuser3 PASSWORD 'md5fe54c4f3129f2a766f53e4f4c9d2a698' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;

:

      to delete an PostgreSQL user account, it should be noted that only the superuser or the user with CREATEROLE privileges can execute this command. If the superuser is to be deleted, the command can only be executed as the superuser. This command is used as follows:
 


    dropuser [option...] [username]
 

      1. List of command line options:

 

选项 说明
-e(--echo) 回显dropuser生成的命令并且把它发送到服务器。
-i(--interactive) 在做任何破坏性动作前提示。
-h(--host=host) 指定PostgreSQL服务器的主机名。
-p(--port=port) 指定服务器的监听端口,如不指定,则为缺省的5432。
-U(--username=username) 本次操作的登录用户名。

  2. Example of application:
 


    # Directly deletes the specified user.
    /> dropuser -e myuser3
    DROP ROLE myuser3;
    # When a specified user is deleted, the command provides a prompt to avoid misoperation.
    /> dropuser -e -i myuser2
    Role "myuser2" will be permanently removed.
    Are you sure? (y/n) y
    DROP ROLE myuser2;
 


Related articles: