psql常见用法

# ps -ef |grep postgres
root      5371  5336  0 08:58 pts/2    00:00:00 grep --color=auto postgres
postgres 21875     1  0 May26 ?        00:00:00 postgres -D /opt/data6000/
postgres 21876 21875  0 May26 ?        00:00:00 postgres: logger
postgres 21878 21875  0 May26 ?        00:00:00 postgres: checkpointer
postgres 21879 21875  0 May26 ?        00:00:00 postgres: background writer
postgres 21880 21875  0 May26 ?        00:00:00 postgres: walwriter
postgres 21881 21875  0 May26 ?        00:00:01 postgres: autovacuum launcher
postgres 21882 21875  0 May26 ?        00:00:01 postgres: stats collector
# sh psql.sh
Last login: Wed May 26 18:11:22 CST 2021 on pts/1
$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

$ psql
psql (12.7)
Type "help" for help.

**postgres=# \l**
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=# \h
Available help:
  ABORT                            CHECKPOINT                       CREATE USER                      DROP TRIGGER
  ALTER AGGREGATE                  CLOSE                            CREATE USER MAPPING              DROP TYPE
  ALTER COLLATION                  CLUSTER                          CREATE VIEW                      DROP USER
  ALTER CONVERSION                 COMMENT                          DEALLOCATE                       DROP USER MAPPING
  ALTER DATABASE                   COMMIT                           DECLARE                          DROP VIEW
  ALTER DEFAULT PRIVILEGES         COMMIT PREPARED                  DELETE                           END
  ALTER DOMAIN                     COPY                             DISCARD                          EXECUTE
  ALTER EVENT TRIGGER              CREATE ACCESS METHOD             DO                               EXPLAIN
  ALTER EXTENSION                  CREATE AGGREGATE                 DROP ACCESS METHOD               FETCH
  ALTER FOREIGN DATA WRAPPER       CREATE CAST                      DROP AGGREGATE                   GRANT
  ALTER FOREIGN TABLE              CREATE COLLATION                 DROP CAST                        IMPORT FOREIGN SCHEMA
  ALTER FUNCTION                   CREATE CONVERSION                DROP COLLATION                   INSERT
  ALTER GROUP                      CREATE DATABASE                  DROP CONVERSION                  LISTEN
  ALTER INDEX                      CREATE DOMAIN                    DROP DATABASE                    LOAD
  ALTER LANGUAGE                   CREATE EVENT TRIGGER             DROP DOMAIN                      LOCK
  ALTER LARGE OBJECT               CREATE EXTENSION                 DROP EVENT TRIGGER               MOVE
  ALTER MATERIALIZED VIEW          CREATE FOREIGN DATA WRAPPER      DROP EXTENSION                   NOTIFY
  ALTER OPERATOR                   CREATE FOREIGN TABLE             DROP FOREIGN DATA WRAPPER        PREPARE
  ALTER OPERATOR CLASS             CREATE FUNCTION                  DROP FOREIGN TABLE               PREPARE TRANSACTION
  ALTER OPERATOR FAMILY            CREATE GROUP                     DROP FUNCTION                    REASSIGN OWNED
  ALTER POLICY                     CREATE INDEX                     DROP GROUP                       REFRESH MATERIALIZED VIEW
  ALTER PROCEDURE                  CREATE LANGUAGE                  DROP INDEX                       REINDEX
  ALTER PUBLICATION                CREATE MATERIALIZED VIEW         DROP LANGUAGE                    RELEASE SAVEPOINT
  ALTER ROLE                       CREATE OPERATOR                  DROP MATERIALIZED VIEW           RESET
  ALTER ROUTINE                    CREATE OPERATOR CLASS            DROP OPERATOR                    REVOKE
  ALTER RULE                       CREATE OPERATOR FAMILY           DROP OPERATOR CLASS              ROLLBACK
  ALTER SCHEMA                     CREATE POLICY                    DROP OPERATOR FAMILY             ROLLBACK PREPARED
  ALTER SEQUENCE                   CREATE PROCEDURE                 DROP OWNED                       ROLLBACK TO SAVEPOINT
  ALTER SERVER                     CREATE PUBLICATION               DROP POLICY                      SAVEPOINT
  ALTER STATISTICS                 CREATE ROLE                      DROP PROCEDURE                   SECURITY LABEL
  ALTER SUBSCRIPTION               CREATE RULE                      DROP PUBLICATION                 SELECT
  ALTER SYSTEM                     CREATE SCHEMA                    DROP ROLE                        SELECT INTO
  ALTER TABLE                      CREATE SEQUENCE                  DROP ROUTINE                     SET
  ALTER TABLESPACE                 CREATE SERVER                    DROP RULE                        SET CONSTRAINTS
  ALTER TEXT SEARCH CONFIGURATION  CREATE STATISTICS                DROP SCHEMA                      SET ROLE
  ALTER TEXT SEARCH DICTIONARY     CREATE SUBSCRIPTION              DROP SEQUENCE                    SET SESSION AUTHORIZATION
  ALTER TEXT SEARCH PARSER         CREATE TABLE                     DROP SERVER                      SET TRANSACTION
  ALTER TEXT SEARCH TEMPLATE       CREATE TABLE AS                  DROP STATISTICS                  SHOW
  ALTER TRIGGER                    CREATE TABLESPACE                DROP SUBSCRIPTION                START TRANSACTION
  ALTER TYPE                       CREATE TEXT SEARCH CONFIGURATION DROP TABLE                       TABLE
  ALTER USER                       CREATE TEXT SEARCH DICTIONARY    DROP TABLESPACE                  TRUNCATE
  ALTER USER MAPPING               CREATE TEXT SEARCH PARSER        DROP TEXT SEARCH CONFIGURATION   UNLISTEN
  ALTER VIEW                       CREATE TEXT SEARCH TEMPLATE      DROP TEXT SEARCH DICTIONARY      UPDATE
  ANALYZE                          CREATE TRANSFORM                 DROP TEXT SEARCH PARSER          VACUUM
  BEGIN                            CREATE TRIGGER                   DROP TEXT SEARCH TEMPLATE        VALUES
  CALL                             CREATE TYPE                      DROP TRANSFORM                   WITH
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
$ psql
psql (12.7)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=# \d
          List of relations
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | company | table | postgres
 public | student | table | postgres
(2 rows)

postgres=# \c db1
You are now connected to database "db1" as user "postgres".
db1=# \h create database
Command:     CREATE DATABASE
Description: create a new database
Syntax:
CREATE DATABASE name
    [ [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ]
           [ IS_TEMPLATE [=] istemplate ] ]

URL: https://www.postgresql.org/docs/12/sql-createdatabase.html

db1=# \d
Did not find any relations.
db1=# create table t (id int);
CREATE TABLE
db1=# \d
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | t    | table | postgres
(1 row)

db1=# \d+
                    List of relations
 Schema | Name | Type  |  Owner   |  Size   | Description
--------+------+-------+----------+---------+-------------
 public | t    | table | postgres | 0 bytes |
(1 row)

db1=# \d+ t
                                     Table "public.t"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
 id     | integer |           |          |         | plain   |              |
Access method: heap

db1=# \timing
Timing is on.
db1=# select count(*) from t;
 count
-------
     0
(1 row)

Time: 0.963 ms
db1=# \dn
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 row)

db1=# \db
       List of tablespaces
    Name    |  Owner   | Location
------------+----------+----------
 pg_default | postgres |
 pg_global  | postgres |
(2 rows)

db1=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

db1=# \dg
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

db1=# \?
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview [COLUMNS] execute query and display results in crosstab
  \errverbose            show most recent error message at maximum verbosity
  \g [FILE] or ;         execute query (and send results to file or |pipe)
  \gdesc                 describe result of query, without executing it
  \gexec                 execute query, then execute each value in its result
  \gset [PREFIX]         execute query and store results in psql variables
  \gx [FILE]             as \g, but forces expanded output mode
  \q                     quit psql
  \watch [SEC]           execute query every SEC seconds

Help
  \? [commands]          show help on backslash commands
  \? options             show help on psql command-line options
  \? variables           show help on special variables
  \h [NAME]              help on syntax of SQL commands, * for all commands

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
db1=# \d
information_schema.  pg_temp_1.           pg_toast_temp_1.     t
pg_catalog.          pg_toast.            public.

db1=# \set autocommit off;
db1=# \set autocommit on;
db1=#
上一篇:PostgreSQL 入门实践


下一篇:【Laravel3.0.0源码阅读分析】Postgres连接类