Linux安装PostgreSQL数据库
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.
本文先使用命令行工具操作PostgreSQL
,然后再使用Python客户端和Go客户端演示。
如果使用的是Ubuntu
或Debian
,直接使用apt-get install
安装postgresql
和postgresql-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
的结构,类似MySQL
的DESC
c [databasename]
连接到指定数据库\q
退出数据库
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK