在Mac上安装和使用PostgreSQL的基本方法
文章摘要
我在Mac上安装了PostgreSQL,并整理了基本的使用方法。
這本書的章節
确认Homebrew
brew --version
# Homebrew 3.6.17
# Homebrew/homebrew-core (git revision 04d0476d1de; last commit 2023-01-02)
# Homebrew/homebrew-cask (git revision 9a37837650; last commit 2023-01-02)
确认可安装的PostgreSQL版本
brew search postgresql
#==> Formulae
#postgresql@10 postgresql@12 postgresql@14 ✔ postgresql@9.4 qt-postgresql
#postgresql@11 postgresql@13 postgresql@15 postgresql@9.5 postgrest
#==> Casks
#navicat-for-postgresql
#If you meant "postgresql" specifically:
#postgresql breaks existing databases on upgrade without human intervention.
#See a more specific version to install with:
# brew formulae | grep postgresql@
默认安装了带有标记的PostgreSQL@14。如果要指定版本,请使用postgresql@版本执行以下操作。
安装PostgreSQL。
brew install postgresql
#Running `brew update --auto-update`...
#==> Auto-updated Homebrew!
#Updated 3 taps (homebrew/core, homebrew/cask and homebrew/services).
#==> New Formulae
#・・・
进行版本确认。
psql --version
#psql (PostgreSQL) 14.6 (Homebrew)
启动PostgreSQL
brew services start postgresql
#Warning: Use postgresql@14 instead of deprecated postgresql
#==> Successfully started `postgresql@14` (label: #homebrew.mxcl.postgresql@14)
连接到PostgreSQL
连接到PostgreSQL时,
可以使用命令“psql -h 主机名/IP地址 -p 端口号 -U 用户名 -d 数据库名”。
主机名/IP地址默认为localhost(本地主机),
端口号默认为5432,
用户名默认为操作系统的用户名(超级用户),
数据库名默认为postgres。
psql -h localhost -p 5432 -U OSのユーザー名 -d postgres
#psql (14.6 (Homebrew))
#Type "help" for help.
#postgres=#
经常使用的命令
创建卷
无论在连接到PostgreSQL之前还是之后,都可以创建角色。本次将省略授予权限的步骤。
如果在连接之前创建角色时需要设置密码,可以使用命令“createuser -P 角色名”。
createuser -P niwa
#Enter password for new role:
#Enter it again:
在连接之后的情况下
create role teru with login password 'aki';
#CREATE ROLE
与创建角色类似,我们可以在连接到PostgreSQL之前,也就是通过Shell执行各种命令。但本文章仅限于介绍连接到PostgreSQL后的命令。
创建数据库
CREATE DATABASE db;
#CREATE DATABASE
创建桌子
在已创建的数据库(db)下创建表格。
psql db
#psql (14.6 (Homebrew))
#Type "help" for help.
CREATE TABLE "営業拠点マスター"
("営業所番号" text not null,
"営業拠点" text not null,
"郵便番号" text not null,
"住所" text not null,
PRIMARY KEY("営業所番号"));
#CREATE TABLE
插入桌子
INSERT INTO "営業拠点マスター"("営業所番号","営業拠点","郵便番号","住所") VALUES ('JP_BO_1','札幌支店','065-0033','札幌市東区北33条東5丁目1番28号');
#INSERT 0 1
获取表格数据
select * from "営業拠点マスター";
# 営業所番号 | 営業拠点 | 郵便番号 | 住所
#------------+----------+----------+--------------------------------
# JP_BO_1 | 札幌支店 | 065-0033 | 札幌市東区北33条東5丁目1番28号
#(1 row)
数据库的列表显示
\l
# List of databases
# Name | Owner | Encoding | Collate | Ctype | Access privileges
#-----------+----------+----------+---------+-------+-----------------------
# db | jp420521 | UTF8 | C | C |
# postgres | OSのユーザー名 | UTF8 | C | C |
# template0 | OSのユーザー名 | UTF8 | C | C | =c/OSのユーザー名 +
# | | | | | OSのユーザー名=CTc/OSのユーザー名
# template1 | OSのユーザー名 | UTF8 | C | C | =c/OSのユーザー名 +
# | | | | | OSのユーザー名=CTc/OSのユーザー名
展示卷的列表
\du
# List of roles
# Role name | Attributes | Member of
#-----------+------------------------------------------------------------+-----------
# OSのユーザー名 | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
# niwa | | {}
# teru | | {}
列出模式列表
\dn
# List of schemas
# Name | Owner
#--------+----------
# public | jp420521
#(1 row)
桌子的列表展示 de
\dt
# List of relations
# Schema | Name | Type | Owner
#--------+------------------+-------+----------
# public | 営業拠点マスター | table | jp420521
#(1 row)
##テーブルの詳細表示
\d '営業拠点マスター'
# Table "public.営業拠点マスター"
# Column | Type | Collation | Nullable | Default
#------------+------+-----------+----------+---------
# 営業所番号 | text | | not null |
# 営業拠点 | text | | not null |
# 郵便番号 | text | | not null |
# 住所 | text | | not null |
#Indexes:
# "営業拠点マスター_pkey" PRIMARY KEY, btree ("営業所番号")