PostgreSQL installation and usage
PostgreSQL is an open source relational Database management system. The following details how to install and use PostgreSQL, how to create data tables, and how to insert, modify, query, and delete data.
1. Install PostgreSQL:
-On the official website( https://www.postgresql.org/ )Download the installation package suitable for your operating system.
-Run the installation program and follow the instructions to install. During the installation process, you can choose to install the accompanying graphical interface tool pgAdmin to facilitate database management.
2. Start the PostgreSQL service:
-Find and run the pgAdmin tool in the installation directory.
-In the server window on the left, select the "PostgreSQL" server, right-click and select "Connect Server" to connect to the database server.
-After entering the password (if set), you will see that you have connected to the server.
3. Create a database:
-In the pgAdmin tool, expand the server window and right-click on the "Databases" folder, select "New Database" to create a new database.
-Enter the database name, select the required Character encoding, collation and template, and then click the "Save" button to create the database.
4. Create a data table:
-In the pgAdmin tool, expand the database window, locate the database where you want to create the table, right-click, and select "Query Tool" to open the query tool.
-In the query tool, enter the following SQL statement to create a data table named 'users':
sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
age INT
);
This statement creates a data table containing columns for id, name, and age. The id column is the primary key and is automatically incremented.
5. Data insertion:
-In the query tool, enter the following SQL statement to insert a new piece of data:
sql
INSERT INTO users (name, age)
VALUES ('John Doe', 25);
This statement will insert a new record named "John Doe" in the users table, aged 25.
6. Data modification:
-In the query tool, enter the following SQL statement to modify the records in the data table:
sql
UPDATE users
SET age = 30
WHERE name = 'John Doe';
This statement will change the age of the record named "John Doe" to 30.
7. Data Query:
-In the query tool, enter the following SQL statement to query the records in the data table:
sql
SELECT * FROM users;
This statement will return all records in the users table.
8. Data deletion:
-In the query tool, enter the following SQL statement to delete records in the data table:
sql
DELETE FROM users
WHERE age > 30;
This statement will delete all records in the users table that are older than 30.
These are the basic operations for using PostgreSQL. You can further apply and develop by learning more SQL syntax and PostgreSQL related features.