2

Linux安装PostgreSQL数据库

 2 years ago
source link: https://allenwind.github.io/blog/5530/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文先使用命令行工具操作PostgreSQL,然后再使用Python客户端和Go客户端演示。

如果使用的是UbuntuDebian,直接使用apt-get install安装postgresqlpostgresql-client即可。

sudo apt-get install postgresql
sudo apt-get install postgresql-client

当安装完后,系统默认生成一个名为postgres的空数据库和一个名为postgres的数据库用户和Linux系统用户。如果你使用Windows这个过程就更直观了。在Windows下推荐使用HeidiSQL这个关系型数据库图形用户界面。当然,Windows下安装本数据库是默认自带图形界面pgAdmin 4

添加用户和数据库

  • 第一种方法是切换到postgres用户,然后直接登录。
sudo su - postgres

然后直接使用客户端登录。

psql

由于是postgresql的默认用户,所以登录是不用口令。现在根据提示修改密码。

postgre=#\password postgres

修改完密码后下次直接使用shell命令即可登录,而不用切换用户。

psql -U postgres -h localhost
password: ****
  • 第二种方法使用shell

登录数据库

MySQL类似,PostgreSQL的登录命令如下:

psql -U postgres -d dbname -h localhost -p 5432

然后输入密码就可以登录数据库。有几个细节需要注意,如果指定的数据库并没有创建,则登录失败;另外,默认情况是登录本地数据库,则不用指定IP和端口号。

控制台常用命令

PostgreSQL的控制台命令都是以/开头

  • \l 显示数据库列表
  • \d 显示当前数据库的所有table
  • \d [tablename] 显示指定table的结构,类似MySQLDESC
  • c [databasename] 连接到指定数据库
  • \q 退出数据库

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK