0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

メモ: PostgreSQL

0
Last updated at Posted at 2019-12-12

参考

手順

sudo apt-get install curl ca-certificates gnupg
# 鍵の追加
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# リポジトリの追加
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# 最新の12をインストール
sudo apt install -y postgresql-12

# 起動
sudo systemctl start postgresql

接続

sudo -s
# postgresユーザーになる
su - postgres

# postgresユーザとしてDBにログイン
psql
  • デフォルトのポート番号は 5432

操作

DB作成
# DB一覧確認
psql -l

# DB作成
createdb test1

# DB一覧確認
psql -l
# リストア
psql dbname < infile

# リストア (pg_restore)
pg_restore -d test1 /tmp/infile
# show tablesの代わり
\dt

# 縦に表示
\x
select * from table1;

ホームディレクトリ

  • /var/lib/postgresql/

その他

  • サーバーへの接続方法
psql -U postgres -d postgres -h postgres13
  • 接続上の注意
    • postgresql 13にはpostgresql 12からしか接続ができない(OIDがなくなったため。)

forumdb=# \d categories
ERROR: column c.relhasoids does not exist
LINE 1: ..., c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoi...

-- テーブルの作成方法
CREATE TABLE table1 (id integer, name varchar(10));
  • 設定ファイル
    • ~/.psqlrc
docker-compose.yml
kikuchi@kvm:~/a/postgres$ cat docker-compose.yml
version: '3'

services:
  p13:
    image: postgres:13
    container_name: postgres13
    environment:
      - POSTGRES_PASSWORD=password
  p12:
    image: postgres:12
    container_name: postgres12
    environment:
      - POSTGRES_PASSWORD=password
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?