Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/e02b99a49877bb6d572e2e3c97205337 to your computer and use it in GitHub Desktop.
Save kenmori/e02b99a49877bb6d572e2e3c97205337 to your computer and use it in GitHub Desktop.
PostgreSQLをすぐに始めたい人の環境構築手順方法 on Mac(Getting start for those who want to start PostgreSQL immediately)

PostgreSQLをすぐに始めたい人の環境構築手順方法 on Mac (Getting start for those who want to start PostgreSQL immediately)

わかりみSQL

  • install Docker App for Descktop
  • start Docker
  • hit command below

1.Dwonload from Docker Hubで登録してデスクトップのDockerアプリをダウンロード

https://docs.docker.com/docker-for-mac/install/

2. docker pull postgres:14

14が適切かどうかは都度確認しましょう。 docker postgres 最新をinstallしましょう

3. docker run --name postgre14 -d -e POSTGRES_PASSWORD=pass1 postgres:14

❯ docker run --name postgre14 -d -e POSTGRES_PASSWORD=pass1 postgres:14
bc6874d3b41013e1436239bb19712708a0b8705893a20931441ea05524e8752b

4. docker ps 確認

❯ docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED              STATUS              PORTS      NAMES
bc6874d3b410   postgres:14   "docker-entrypoint.s…"   About a minute ago   Up About a minute   5432/tcp   postgre14
~ ❯

5. docker exec -it postgre14 bash コンテナにアクセス

❯ docker exec -it postgre14 bash
root@bc6874d3b410:/#

6. which psql createuser createdb コマンドの存在を確認

root@bc6874d3b410:/# which psql createuser createdb
/usr/bin/psql
/usr/bin/createuser
/usr/bin/createdb

7. createuser -U postgres user1 データベースユーザ user1を作成

root@bc6874d3b410:/# createuser -U postgres user1
root@bc6874d3b410:/#

8. psql -U postgres -c '\du' 作成されたことを確認

                                   List of roles
 Role name |                         Attributes                         | Member
 of
-----------+------------------------------------------------------------+-------
----
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 user1     |                                                            | {}

9. createdb -U postgres -O user1 -E UTF8 --locale=C -T template0 testdb1 データベース「testdb1」を作成

root@bc6874d3b410:/# createdb -U postgres -O user1 -E UTF8 --locale=C -T template0 testdb1
root@bc6874d3b410:/#

10. psql -U postgres -l

  Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 testdb1   | user1    | UTF8     | C          | C          |
(4 rows)

testdb1 が作られている

11. vimを入れる

apt-get update
apt-get install vim

すると

root@bc6874d3b410:/# apt-get update
apt-get install vim
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB]
Get:3 http://deb.debian.org/debian bullseye/main arm64 Packages [8,068 kB]
Get:4 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
Get:5 http://security.debian.org/debian-security bullseye-security/main arm64 Packages [95.0 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main arm64 Packages [2,600 B]
Get:7 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg InRelease [86.7 kB]
Get:8 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg/main arm64 Packages [227 kB]
Fetched 8,679 kB in 5s (1,921 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libgpm2 vim-common vim-runtime xxd
Suggested packages:
  gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
  libgpm2 vim vim-common vim-runtime xxd
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 8,066 kB of archives.
After this operation, 37.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

yを押す

Get:1 http://deb.debian.org/debian bullseye/main arm64 xxd arm64 2:8.2.2434-3 [192 kB]
.
.
.

12. もう一度データベースに入る

  • psql -U user1 testdb1
root@bc6874d3b410:/# psql -U user1 testdb1
psql (14.1 (Debian 14.1-1.pgdg110+1))
Type "help" for help.

testdb1=>

\qで戻り

vim create-testtable.sql

create table testtable1 (id integer primary key , name text not null unique , age integer );

として編集 閉じて

psql -U user1 testdb1

入って以下を実行

testdb1=> \i create-testtable.sql
CREATE TABLE
testdb1=>

作られた

確認

testdb1=> \dt
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+-------
 public | testtable1 | table | user1
(1 row)

付録: Postgresアプリをダウンロード

https://postgresapp.com/downloads.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment