我是一名postgreSQL新手,我找不到任何关于使用psql命令的有用介绍.至少我认为这是我想要使用的命令.

在postgreSQL中,是否可以简单地连接到服务器,然后列出、创建、使用和判断数据库?

我希望能够使用psql在MySQL上做类似的事情(我删除了许多额外的行):

在不指定数据库的情况下进行连接-我似乎无法使用psql进行连接:

$ mysql -u root -prootpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Server version: 5.5.28 MySQL Community Server (GPL)

我可以用mysql列出数据库,但posgreSQL命令SHOW似乎无法做到这一点.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ocdp               |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.04 sec)

我可以切换数据库.

mysql> use ocdp;
Database changed

我无法理解psql中的这个命令:

mysql> show tables;
+---------------------------------+
| Tables_in_ocdp                  |
+---------------------------------+
| OCAddresses                     |
| OCStreets                       |
+---------------------------------+
2 rows in set (0.00 sec)

我想我可以在psql中使用'createdb'和'dropdb'命令来实现这一点:

mysql> create database foo;
Query OK, 1 row affected (0.00 sec)

mysql> drop database foo;
Query OK, 0 rows affected (0.03 sec)

我使用\退出

mysql> quit
Bye

对于了解postgreSQL的人来说,这些问题的答案应该只需要一点时间,但我在任何地方都找不到演示如何执行这些简单操作的文档.也许我根本不应该用psql来做这个?

推荐答案

连接到服务器:

$ mysql -u root -prootpassword

$ su - postgres
$ psql

列出数据库:

mysql> show databases;

postgres=# \l

切换数据库:

mysql> use ocdp;

postgres=# \c ocdp

显示表格:

mysql> show tables;

postgres=# \dt

创建数据库:

mysql> create database foo;

postgres=# create database foo;

删除数据库:

mysql> drop database foo;

postgres=# drop database foo;

退出:

mysql> quit

postgres=# \q

Postgresql相关问答推荐

如何在PSQL中查询jsonb列包含字符串的嵌套数组

Postgres 11没有强制执行外键约束?

对 VOLATILE 函数的调用会 destruct SELECT 语句的原子性

Postgres数据库系统已准备好接受连接和docker compose

如何在不同的行中显示两列,避免重复并省略空条目

如何查找具有包含花括号内值且遵循模式的属性的顶点?

当我写 SELECT ~1;在 Postgresql 上它给了我 -2 结果.这是什么原因?它一直持续〜4和-5等

Postgres 根据自己的估计 Select 一个更费时的查询计划

是否有 postgres CLOSEST 运算符?

Windows PSQL 命令行: is there a way to allow for passwordless login?

更新列或列或列时触发触发

在 postgres 中Decode解码相似的函数

如何在容器内创建 postgres 扩展?

psql:致命:connection requires a valid client certificate

没有查询要创建的in ... error

Postgres 表中列的顺序会影响性能吗?

用于更改 postgresql 用户密码的 bash 脚本

PostgreSQL 中跨多个表的索引

重命名 Amazon RDS 主用户名

Rails 5 db 迁移:如何修复 ActiveRecord::ConcurrentMigrationError