ローカル環境でPostgreSQLを始める:pgAdmin、コマンドライン、シェルを使いこなす
pgAdminで新しいローカルサーバーを作成する方法
前提条件:
- PostgreSQLがインストールされていること
手順:
- pgAdminを起動します。
- 左側のツリーパネルで、**「サーバー」**ノードを右クリックします。
- **「新規サーバーの登録」**を選択します。
- **「一般」**タブで、新しいサーバーの名前を入力します。これは、pgAdmin内でサーバーを識別するために使用する名前です。
- **「接続」**タブに切り替え、次の情報を入力します。
- ホスト名/アドレス: これは、PostgreSQLサーバーが実行されているコンピューターのアドレスです。ローカルサーバーの場合は、通常「localhost」または「127.0.0.1」を入力します。
- ポート: PostgreSQLサーバーのポート番号です。デフォルトは5432ですが、インストール時に変更している場合は、その番号を入力する必要があります。
- データベース: 接続するデータベースの名前です。空のままにしておくと、デフォルトのデータベースに接続されます。
- パスワード: ユーザー名のパスワードです。
- **「保存」**ボタンをクリックします。
新しいサーバーがpgAdminツリーパネルに表示されます。サーバーをダブルクリックすると、そのサーバー内のデータベースとその他のオブジェクトを表示できます。
ヒント:
- SSLで保護された接続を使用している場合は、**「接続」**タブで追加設定が必要になります。
- 複数のユーザーアカウントがある場合は、それぞれのユーザーアカウント用の接続を作成できます。
- サーバーを作成した後、**「ツール」メニューから「接続プロパティ」**を選択して、接続プロパティを変更できます。
トラブルシューティング:
- pgAdminにサーバーが表示されない場合は、接続情報が正しいことを確認してください。
- 接続に失敗する場合は、PostgreSQLサーバーが起動していることを確認してください。
- ユーザー名またはパスワードが間違っている場合は、PostgreSQLサーバーの認証設定を確認する必要があります。
# This is a sample code for creating a new local server in pgAdmin
# Import the libraries
import psycopg2
# Define the connection parameters
hostname = "localhost"
port = 5432
database = "postgres"
username = "postgres"
password = "password"
# Connect to the PostgreSQL server
try:
connection = psycopg2.connect(host=hostname, port=port, database=database, user=username, password=password)
print("Connected to PostgreSQL server")
except psycopg2.Error as e:
print("Failed to connect to PostgreSQL server:", e)
# Create a new cursor
cursor = connection.cursor()
# Create a new database
database_name = "my_new_database"
cursor.execute(f"CREATE DATABASE {database_name}")
connection.commit()
print(f"Created new database: {database_name}")
# Close the cursor and connection
cursor.close()
connection.close()
- Import the libraries: This line imports the
psycopg2
library, which is used to connect to PostgreSQL databases. - Define the connection parameters: These variables define the hostname, port, database name, username, and password for the PostgreSQL server.
- Connect to the PostgreSQL server: This code attempts to connect to the PostgreSQL server using the specified connection parameters. If the connection is successful, it prints a message to the console. If the connection fails, it prints an error message to the console.
- Create a new cursor: A cursor is used to execute SQL statements against the database.
- Create a new database: This code executes a SQL statement to create a new database named
my_new_database
. - Commit the changes: This code commits the changes made to the database.
- Close the cursor and connection: This code closes the cursor and the connection to the database.
Note: This is just a basic example, and you may need to modify it to fit your specific needs. For example, you may need to change the connection parameters or the name of the new database.
コマンドラインツールを使用する:
PostgreSQL には、createdb
コマンドラインツールが含まれています。このツールを使用して、新しいデータベースを作成できます。
createdb my_new_database
このコマンドは、my_new_database
という名前の新しいデータベースを作成します。
PostgreSQLシェルは、データベースと対話するためのインタラクティブなツールです。シェルを使用して、新しいデータベースを作成できます。
psql -U postgres
CREATE DATABASE my_new_database;
このコマンドは、postgres
ユーザーとして PostgreSQLシェルにログインし、my_new_database
という名前の新しいデータベースを作成します。
Dockerを使用する:
Docker は、コンテナー化されたアプリケーションをデプロイおよび管理するためのプラットフォームです。Dockerを使用して、PostgreSQLコンテナーを実行し、新しいデータベースを作成できます。
docker run --name my-postgres -e POSTGRES_PASSWORD=password -p 5432:5432 postgres
このコマンドは、my-postgres
という名前の新しい Dockerコンテナーを起動し、パスワード password
を使用して PostgreSQLサーバーを実行します。コンテナーはポート 5432 で公開されるため、pgAdminを使用して接続できます。
- pgAdmin: pgAdminは、グラフィカルユーザーインターフェースを備えた使いやすいツールです。初心者や、データベース管理に慣れていないユーザーにとって良い選択です。
- コマンドラインツール: コマンドラインツールは、迅速かつ効率的にタスクを実行するのに適しています。データベース管理に精通しているユーザーにとって良い選択です。
- PostgreSQLシェル: PostgreSQLシェルは、データベースと対話するための柔軟で強力なツールです。データベース管理に精通しているユーザーにとって良い選択です。
- Docker: Dockerは、コンテナー化されたアプリケーションを簡単にデプロイおよび管理する必要がある場合に適しています。
postgresql pgadmin