1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PostgreSQL】環境構築

1
Last updated at Posted at 2026-06-25

やりたいこと

  • wsl上でPostgreSQLのサンプルコードを動かす

導入

Ubuntu-24.04
$ sudo apt update
$ sudo apt install postgresql postgresql-contrib
$ psql --version

サーバ起動

$ sudo service postgresql start   # 起動
$ sudo service postgresql status  # 確認, active (running)なら起動中

実装

test.sql
-- データベース作成
CREATE DATABASE sampledb;

-- データベース接続
\c sampledb

-- テーブル作成
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT,
    age INT
);

-- データ追加
INSERT INTO users (name, age)
VALUES
('Masaki', 22),
('Taro', 25);

-- データ参照
SELECT * FROM users;    -- SELECT id, name, age FROM users;と同義

実行

$ pwd
/home/masaki/java
$ sudo -u postgres psql -f test.sql  -- postgresユーザで,psqlを使用してtest.sqlを実行

結果

CREATE DATABASE
You are now connected to database "sampledb" as user "postgres".
CREATE TABLE
INSERT 0 2
 id |  name  | age 
----+--------+-----
  1 | Masaki |  22
  2 | Taro   |  25
(2 rows)

サーバ停止

$ sudo service postgresql stop    # 停止
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?