1
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 1 year has passed since last update.

PostgreSQLでデータベースを作ってテーブルにデータを入れて取得してみた

1
Posted at

概要

今日はデータベースの勉強ということで、PostgreSQLで遊んでみました。
インストールして、データベースを作って、テーブルを作って、データを入れて、取得するところまでやってみました。
以下のページを参考にしました。
https://www.javadrive.jp/postgresql/

環境構築

以下のコマンドを実行しました。

$ sudo apt install postresql

postgresでログイン

以下のコマンドを実行しました。

$ sudo su postgres -c 'psql --username=postgres'

これでpostgresとしてログインできるので、ユーザーを作りました。user2という名前です。create userを実行しました。

postgres=# CREATE USER user2 WITH PASSWORD '********';
CREATE ROLE

続いてデータベースを作りました。create databaseを実行しました。

postgres=# CREATE DATABASE mydb;
CREATE DATABASE

下準備はできました。

user2でログイン

別端末から以下のコマンドを実行しました。さきほど作ったmydbにuser2でアクセスしています。

$ psql -U user2 -h localhost -d mydb

テーブルmybookを作成しました。create tableを実行しました。

mydb=> create table mybook (
  id integer, 
  name varchar(10)
);
CREATE TABLE

データを投入しました。insert文を実行しました。

mydb=> insert into mybook values (11, 'hoge');
INSERT 0 1

データが入ったことを確認しました。select文を実行しました。

mydb=> select id, name from mybook;
 id | name 
----+------
 11 | hoge
(1 row)

何かの役に立てばと。

1
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
1
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?