To install PostgreSQL and PostGIS on CentOS using `yum`, follow these steps:
1. Update Package Lists:
Before installing any packages, it's a good practice to update the package lists:
```bash
sudo yum update
```
2. Install PostgreSQL:
PostgreSQL is available in the default CentOS repositories. To install PostgreSQL, run the following command:
```bash
sudo yum install postgresql-server postgresql-contrib
```
3. Initialize PostgreSQL Database:
After installing PostgreSQL, initialize the database and start the PostgreSQL service:
```bash
sudo postgresql-setup initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql
```
4. Install PostGIS:
PostGIS is available in the EPEL (Extra Packages for Enterprise Linux) repository. If you haven't already enabled the EPEL repository, do so by running:
```bash
sudo yum install epel-release
```
Then, install PostGIS:
```bash
sudo yum install postgis postgresql-contrib
```
During the installation, it will automatically pick the correct version of PostGIS that is compatible with your installed PostgreSQL version.
5. Enable PostGIS Extension:
Connect to your PostgreSQL database using the `psql` command-line tool:
```bash
sudo -u postgres psql -d your_database
```
Replace `your_database` with the name of the database where you want to enable PostGIS.
6. Enable PostGIS Extension:
Once connected to the database, run the following command to enable the PostGIS extension:
```sql
CREATE EXTENSION postgis;
```
This command will create the necessary functions and types in the database to support PostGIS.
7. Verify PostGIS Installation:
You can verify that PostGIS is installed and enabled by running the following SQL query inside the `psql` prompt:
```sql
SELECT postgis_full_version();
```
This query will display the PostGIS version information if the installation was successful.
That's it! PostgreSQL and PostGIS should now be installed and enabled on your CentOS system. You can start using PostGIS to work with geospatial data in your PostgreSQL database. Remember to adjust the database name (`your_database`) according to your specific setup.
pg 设置远程访问
After installing PostgreSQL on CentOS using `yum`, you can configure it to allow remote access by making changes to the PostgreSQL configuration and firewall settings. Here's a step-by-step guide to enabling remote access:
1. Edit PostgreSQL Configuration File:
Open the PostgreSQL configuration file `postgresql.conf` using a text editor. The default location of the file is typically `/var/lib/pgsql/data/postgresql.conf`, but it may vary depending on your PostgreSQL version and installation.
Use the following command to open the file with the `nano` text editor (you can use `vi`, `vim`, or any other text editor of your choice):
```bash
sudo nano /var/lib/pgsql/data/postgresql.conf
```
Find the line that starts with `listen_addresses` and uncomment it (remove the `#` symbol at the beginning of the line) if it's commented out. Then, set the value to `'*'` to allow PostgreSQL to listen on all available network interfaces:
```conf
listen_addresses = '*'
```
Save the file and exit the text editor.
2. Edit PostgreSQL pg_hba.conf File:
The `pg_hba.conf` file controls the authentication method for incoming connections to PostgreSQL. Open the file using a text editor:
```bash
sudo nano /var/lib/pgsql/data/pg_hba.conf
```
Add the following line at the end of the file to allow remote access for all databases and all users. Replace `your_remote_ip` with the actual IP address or IP range from which you want to allow connections (e.g., a specific IP address, a subnet like `192.168.1.0/24`, or `0.0.0.0/0` to allow connections from any IP address):
```conf
host all all your_remote_ip/32 md5
```
Save the file and exit the text editor.
3. Restart PostgreSQL:
After making the changes to the configuration files, restart the PostgreSQL service to apply the changes:
```bash
sudo systemctl restart postgresql
```
4. Update Firewall Rules:
If you have a firewall (e.g., `firewalld`) enabled on your CentOS server, you need to allow incoming connections to the PostgreSQL port (default is 5432). For example, if you are using `firewalld`, you can use the following commands:
```bash
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload
```
If you are using a different firewall, you should update the rules accordingly to allow incoming connections to the PostgreSQL port.
After following these steps, your PostgreSQL server should be configured to accept remote connections. Please exercise caution when enabling remote access to a database server, and consider implementing security measures such as strong passwords, SSL encryption, and restricting access to trusted IP addresses to ensure the security of your PostgreSQL instance.
postgresql 设置初始化密码
After installing PostgreSQL on CentOS using `yum`, the default password for the `postgres` user is not set. By default, PostgreSQL on CentOS uses the `ident` authentication method, which means the operating system user `postgres` can log in as the PostgreSQL user `postgres` without a password. So, initially, there is no password required for the `postgres` user.
If you want to set a password for the `postgres` user for enhanced security, you can do so using the following steps:
1. Switch to the `postgres` User:
To set a password for the `postgres` user, you need to switch to the `postgres` user. Open a terminal and run:
```bash
sudo su - postgres
```
This command will switch to the `postgres` user and set up the environment accordingly.
2. Set a Password for the `postgres` User:
After switching to the `postgres` user, use the `psql` command-line tool to set a password for the user. Run:
```bash
psql
```
This will open the `psql` command-line interface connected to the default PostgreSQL database as the `postgres` user.
3. Change the Password:
In the `psql` prompt, use the `\password` command to set a new password for the `postgres` user:
```sql
\password postgres
```
You will be prompted to enter a new password. Type the new password, and then confirm it by typing it again. The password will be masked, and no characters will be shown while you type.
4. Exit `psql`:
After setting the password, type `\q` and press Enter to exit the `psql` prompt.
Now the `postgres` user has a password set. When you or anyone else tries to log in as the `postgres` user using `psql`, PostgreSQL will prompt for the password you set.
Please remember to keep the password secure and make sure to remember it, as you will need it for future login attempts. If you lose the password, you may need to follow other methods for password recovery or resetting.