└── README.md
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | ## Here are the steps to install PostgreSQL on a VPS (Virtual Private Server):
4 |
5 | ## 1. Install PostgreSQL:
6 |
7 | #### 1. Log in to your VPS: SSH into your VPS using the command:
8 |
9 | ```bash
10 | ssh username@your-vps-ip
11 | ```
12 |
13 | #### 2. Update your system: Before installing anything, update your system's packages:
14 |
15 | ```bash
16 | sudo apt update
17 | sudo apt upgrade -y
18 | ```
19 |
20 | #### 3. Install PostgreSQL: Install PostgreSQL and some additional utilities:
21 |
22 | ```bash
23 | sudo apt install postgresql postgresql-contrib -y
24 | ```
25 |
26 | - postgresql installs the core PostgreSQL database system.
27 |
28 | - postgresql-contrib installs extra features and utilities.
29 |
30 | #### 4. Start PostgreSQL service: After installation, the service should start automatically. You can check its status with:
31 |
32 | ```bash
33 | sudo systemctl status postgresql
34 | ```
35 |
36 | If it's not running, you can start it with:
37 |
38 | ```bash
39 | sudo systemctl start postgresql
40 | ```
41 |
42 | ## 2. Create a PostgreSQL Database:
43 |
44 | #### 1. Log in as the PostgreSQL user: PostgreSQL uses a separate user (postgres). Log in as that user:
45 |
46 | ```bash
47 | sudo -i -u postgres
48 | ```
49 |
50 | #### 2. Access the PostgreSQL command line: Launch the PostgreSQL command line tool:
51 |
52 | ```bash
53 | psql
54 | ```
55 |
56 | #### 3. Create a new database: Create a new database by running:
57 |
58 | ```bash
59 | CREATE DATABASE your_database_name;
60 | ```
61 |
62 | #### 4. Create a new user (optional): If you want to create a new user, you can do so with:
63 |
64 | ```bash
65 | CREATE USER your_username WITH PASSWORD 'your_password';
66 | ```
67 |
68 | #### 5. Grant privileges to the user: To allow the new user to access the newly created database, grant them privileges:
69 |
70 | ```bash
71 | GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;
72 | ```
73 |
74 | #### 6. Exit PostgreSQL: After creating the database and user, exit the PostgreSQL shell:
75 |
76 | ```bash
77 | \q
78 | ```
79 |
80 | ## 3. Create a PostgreSQL Database:
81 |
82 | #### 1. Connect to PostgreSQL using the new user: You can now connect to the database with:
83 |
84 | ```bash
85 | psql -U your_username -d your_database_name
86 | ```
87 |
88 | #### 2. Configure PostgreSQL for remote access (optional): If you want to allow remote access to your PostgreSQL server, you need to modify the PostgreSQL configuration files:
89 |
90 | - Open **postgresql.conf** (usually located at **/etc/postgresql/12/main/** or **/etc/postgresql/{version}/main/**):
91 |
92 | ```bash
93 | sudo nano /etc/postgresql/12/main/postgresql.conf
94 | ```
95 |
96 | Find the line with **listen_addresses** and change it to:
97 |
98 | ```bash
99 | listen_addresses = '*'
100 | ```
101 |
102 | - Modify **pg_hba.conf** to allow remote IP access:
103 |
104 | ```bash
105 | sudo nano /etc/postgresql/12/main/pg_hba.conf
106 | ```
107 |
108 | Add the following line to allow all IP addresses (or specify a specific range):
109 |
110 | ```bash
111 | host all all 0.0.0.0/0 md5
112 |
113 | ```
114 |
115 | ### 2. Restart PostgreSQL service: After making changes, restart the PostgreSQL service to apply the configuration:
116 |
117 | ```bash
118 | sudo systemctl restart postgresql
119 | ```
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 | ## To log in to PostgreSQL on your VPS and view databases, tables, and data, you can follow these steps:
128 |
129 | ## 1. Log in to PostgreSQL
130 |
131 | To log into PostgreSQL on your VPS, use the **psql** command. You need to have the **_psql_** client installed on your system. The basic syntax to log in is:
132 |
133 | ```bash
134 | psql -U postgres -h localhost
135 | ```
136 |
137 | ### Here’s the breakdown:
138 |
139 | - **-U postgres:** Specifies the user to log in as (**_postgres_** is the default superuser).
140 |
141 | - **-h localhost:** Specifies the host (if you're logging into a local database, **_localhost_** will work).
142 |
143 | If your PostgreSQL instance is on a remote VPS, you can use its IP address or hostname instead of localhost.
144 |
145 | If you're using a specific database and not the default one, you can specify the database name as follows:
146 |
147 | ```bash
148 | psql -U postgres -h localhost -d your_database_name
149 | ```
150 |
151 | It will prompt you for a password if authentication is required.
152 |
153 | ## 2. List Databases
154 |
155 | Once you're logged into PostgreSQL (**psql**), you can list all databases with the following command:
156 |
157 | ```bash
158 | \l
159 | ```
160 |
161 | This will display all the databases in your PostgreSQL server.
162 |
163 | ## 3. Switch to a Database
164 |
165 | To switch to a particular database, use the following command:
166 |
167 | ```bash
168 | \c your_database_name
169 | ```
170 |
171 | Replace **your_database_name** with the actual database name you want to work with.
172 |
173 | ## 4. List Tables
174 |
175 | To see all the tables in the current database, use the following command:
176 |
177 | ```bash
178 | \dt
179 | ```
180 |
181 | This will list all tables in the current database.
182 |
183 | ## 5. View Table Structure
184 |
185 | To view the structure (schema) of a specific table, you can use:
186 |
187 | ```bash
188 | \d table_name
189 | ```
190 |
191 | Replace **table_name** with the name of the table you want to inspect. This will show you the columns, types, and any constraints on the table.
192 |
193 | ## 5. View Data in a Table
194 |
195 | To view the data in a table, you can run a SELECT query. For example:
196 |
197 | ```bash
198 | SELECT * FROM table_name;
199 | ```
200 |
201 | This will display all rows of data in the specified table. You can also apply filters, like:
202 |
203 | ```bash
204 | SELECT * FROM table_name WHERE column_name = 'value';
205 | ```
206 |
207 | ## 7. Exit PostgreSQL
208 |
209 | To exit the **_psql_** command-line interface, simply type:
210 |
211 | ```bash
212 | \q
213 | ```
214 |
215 | ## Example Workflow
216 |
217 | 1. Log in to PostgreSQL:
218 | ```bash
219 | \q
220 | ```
221 | 2. List databases:
222 | ```bash
223 | \l
224 | ```
225 | 3. Switch to a specific database:
226 | ```bash
227 | \c your_database_name
228 | ```
229 | 4. List all tables:
230 | ```bash
231 | \dt
232 | ```
233 | 5. View table structure:
234 | ```bash
235 | \d your_table_name
236 | ```
237 | 6. View data from a table:
238 | ```bash
239 | SELECT * FROM your_table_name;
240 | ```
241 |
242 | ### That's it! You should now be able to interact with PostgreSQL, view databases, tables, and the data within them. Let me know if you need more help!
243 |
244 |
245 |
246 | # Q. **Fixing PostgreSQL Authentication Issue**
247 |
248 | If you're encountering issues logging into PostgreSQL with the postgres user (or any PostgreSQL user), you can reset the PostgreSQL password for the postgres superuser by following these steps:
249 |
250 | Step-by-Step to Reset PostgreSQL Password:
251 |
252 | ### 1. Switch to the postgres user (the PostgreSQL superuser):
253 |
254 | Run this command to switch to the postgres user, which has superuser privileges within PostgreSQL:
255 |
256 | ```bash
257 | sudo -u postgres psql
258 | ```
259 |
260 | ### 2. Change the PostgreSQL postgres user password:
261 |
262 | Once you're in the PostgreSQL interactive terminal (psql), you can change the password for the postgres user:
263 |
264 | ```bash
265 | ALTER USER postgres WITH PASSWORD 'new_password';
266 | ```
267 |
268 | Replace **new_password** with your desired password.
269 |
270 | ### 3. Exit PostgreSQL:
271 |
272 | To exit the psql prompt, type:
273 |
274 | ```bash
275 | \q
276 | ```
277 |
278 | ### 4. Test the New PostgreSQL Password:
279 |
280 | After changing the password, you can try logging into PostgreSQL again using the new password:
281 |
282 | ```bash
283 | psql -U postgres -h localhost
284 | ```
285 |
286 | You should be prompted for the new password you just set.
287 |
--------------------------------------------------------------------------------