Category: ‘PostgreSQL’

Postgresql でユーザ作成

2023年6月8日 Posted by PURGE

postgres=# create user ***_user with password '******';
CREATE ROLE
postgres=# grant all privileges on database ***_db to ***_user;
GRANT

postgres ユーザで操作する。

2023年6月8日 Posted by PURGE

$ psql postgres
psql (14.8 (Homebrew))
Type "help" for help.

postgres=# 

PostgreSQL の再起動(Mac)

2023年6月8日 Posted by PURGE

$ brew services restart postgresql
Warning: Formula postgresql was renamed to postgresql@14.
Stopping `postgresql@14`... (might take a while)
==> Successfully stopped `postgresql@14` (label: homebrew.mxcl.postgresql@14)
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)

PostgreSQL 基本的コマンド

2018年5月17日 Posted by PURGE

■ postgres ユーザでログイン

> psql -U postgres
ユーザー postgres のパスワード:
psql (10.4)
"help" でヘルプを表示します。

■ PostgreSQLのバージョン確認

postgres=# select version();
                          version
------------------------------------------------------------
 PostgreSQL 10.4, compiled by Visual C++ build 1800, 64-bit
(1 行)

■ データベース一覧表示

postgres=# \l
                                             データベース一覧
   名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |     アクセス権限
-----------+----------+------------------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
(3 行)

■ データベース作成

postgres=# create database sample_db;
CREATE DATABASE

■ データベース一覧表示

postgres=# \l
                                             データベース一覧
   名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |     アクセス権限
-----------+----------+------------------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 sample_db | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
(4 行)

■ データベース削除

postgres=# drop database sample_db;
DROP DATABASE
postgres=# \l
                                             データベース一覧
   名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |     アクセス権限
-----------+----------+------------------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
(3 行)

■ データベース作成 with ユーザ

postgres=# create database sample_db owner sample;
ERROR:  ロール"sample"は存在しません
postgres=# create user sample;
CREATE ROLE
postgres=# create database sample_db owner sample;
CREATE DATABASE
postgres=# alter role sample with password 'password';
ALTER ROLE
postgres=# alter role sample with createdb;
ALTER ROLE