How to Install PostgreSQL on CentOS 7 in 15 Minutes
Ever wanted to quickly spin up your own database server? You've come to the right place. In just 15 minutes, you'll have a PostgreSQL database up and running on your CentOS 7 server. PostgreSQL is a popular open source database, and for good reason. It's fast, reliable, and packed with features. Whether you need a database for a side project, to learn SQL, or to power a production application, PostgreSQL has you covered.
All you need is a CentOS 7 server and a few minutes to follow along. By the end of this guide, you'll have PostgreSQL installed, secured, and ready to start storing and querying data. So pour yourself a coffee, open your terminal, and let's get to it. Your database adventure awaits!
Why Choose PostgreSQL for CentOS 7
Why Choose PostgreSQL for CentOS 7
PostgreSQL is a great open-source database option for CentOS 7. Here are a few reasons why you should consider it:
Security
PostgreSQL has a strong reputation for data safety and security. It supports advanced authentication methods, data encryption, and fine-grained access controls to restrict data access.
Stability
PostgreSQL is very stable and reliable. It has over 25 years of active development and a proven architecture that has stood the test of time. Upgrades between versions are also quite seamless.
Flexibility
PostgreSQL supports a wide range of data types, including JSON, XML, and geospatial data. It also has many advanced features like materialized views, triggers, and stored procedures. You'll have a lot of flexibility in how you model and query your data.
Community Support
PostgreSQL has a very active open-source community. You can easily find help and resources online, whether you have a technical question or want to contribute to the project.
With a proven codebase, flexibility, security, and community support, PostgreSQL is an excellent open-source database for CentOS 7. Give it a try - you won't be disappointed!
Installing PostgreSQL on CentOS 7
To get PostgreSQL up and running on your CentOS 7 server, follow these simple steps:
- Enable the PostgreSQL repository. As root, run:
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- Install PostgreSQL. Run:
yum install -y postgresql12 postgresql12-server
This will install PostgreSQL 12, as well as the server component.
- Initialize the database. Run:
/usr/pgsql-12/bin/postgresql-12-setup initdb
This will set up the initial database environment.
- Start and enable the service. Run:
systemctl start postgresql-12
systemctl enable postgresql-12
This will start the PostgreSQL service and ensure it starts at boot.
- Set a password for the 'postgres' user. Run:
su - postgres
psql
\password postgres
Enter a secure password.
That's it! You now have a PostgreSQL 12 database server installed on your CentOS 7 server. It was ready in 15 minutes - told you it was easy! Now you can create databases, users, and start storing and querying your data. Let the fun begin!
Creating a New Database in PostgreSQL
To create a new database in PostgreSQL, follow these simple steps:
Logging into PostgreSQL
Log into PostgreSQL using the psql command:
```bash
psql -U postgres
```
Creating a database
Once logged in, you can create a new database using the CREATE DATABASE statement:
```sql
CREATE DATABASE mydatabase;
```
Replace mydatabase
with the name you want to give your new database.
Connecting to the database
To connect to your newly created database, use the \c meta-command:
```sql
\c mydatabase
```
Creating tables
You can now create tables, indexes, functions, and more in your database. For example, to create a simple users
table:
```sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT,
age INT
);
```
Adding data
Insert some test data into your users
table:
```sql
INSERT INTO users (name, age) VALUES ('John', 30);
INSERT INTO users (name, age) VALUES ('Jane', 25);
```
That's the basics of creating a new database in PostgreSQL and adding some simple tables and data. Let me know if you have any other questions!
Creating New Users and Granting Privileges
To allow other users to access the database, you'll need to create login roles and grant them privileges. PostgreSQL has a flexible privilege system that allows you to grant specific permissions to users at the database, schema, table, column, and row level.
Creating New Users
To add a new user, connect to the postgres
database and run the CREATE USER
command:
```sql
CREATE USER dbuser WITH PASSWORD 'password';
```
This will create a user named dbuser
with the given password. By default, this user won't have any privileges. You'll need to grant privileges to the user so they can access databases and schemas.
Granting Privileges
To grant privileges to a user, use the GRANT
command. For example, to grant all privileges on a database to a user:
```sql
GRANT ALL PRIVILEGES ON database_name TO dbuser;
```
You can also grant specific privileges like SELECT
, INSERT
, UPDATE
, DELETE
, etc. Privileges can be granted at the database, schema, table, column, and row level for maximum flexibility.
Revoking privileges from a user is done with the REVOKE
command. To revoke all privileges on a database from a user:
```sql
REVOKE ALL PRIVILEGES ON database_name FROM dbuser;
```
With a basic understanding of PostgreSQL's privilege system, you can enable precise access control in your database. Let me know if you have any other questions!
Connecting to the PostgreSQL Database
Once PostgreSQL is installed, you'll want to connect to your new database. There are a few ways to do this:
psql
The psql command line tool lets you interact with PostgreSQL databases directly. To connect, simply run:
```bash
psql -U postgres
```
This will connect you to the default PostgreSQL database as the postgres superuser. You can then create databases, tables, query data, and more.
pgAdmin
pgAdmin is a popular open source administration and development platform for PostgreSQL. You can download pgAdmin and connect to your local PostgreSQL server. pgAdmin provides a graphical interface with tools to create and manage databases, write queries, back up data, and other tasks.
Programming Languages
Most programming languages like Python, Ruby, Java, etc. have libraries to connect to PostgreSQL. You can write scripts and applications to interact with your PostgreSQL databases.
Database Drivers
There are also ODBC and JDBC drivers that let you connect PostgreSQL to business intelligence tools, IDEs, and other software.
With PostgreSQL up and running and so many ways to connect, you'll be querying data and building apps on your new database server in no time! Let me know if you have any other questions.
Conclusion
And there you have it, you now have a fully functioning PostgreSQL database server up and running in just 15 minutes. Not too shabby! With PostgreSQL, you’ve got an open source database that’s powerful, flexible, and has a strong ecosystem of tools to help you manage and interact with your data. Whether you need it for a side project, to support an application in development, or for production use, PostgreSQL can fit the bill. Now you’re ready to start creating databases, tables, loading in data, and running queries. Go have fun and build something awesome! The possibilities are endless. Let me know if you have any other questions about PostgreSQL or database administration. I’m happy to help you on your data journey. If you need a machine you can rent one of our dedicated servers.