js

Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Thursday, March 6, 2025

Install pgAdmin in Ubuntu

 

✅ Install pgAdmin on Ubuntu

To install pgAdmin (a web-based GUI for PostgreSQL) on Ubuntu, follow these steps:


1. Update Package List

sudo apt update
sudo apt upgrade

2. Install Required Packages

sudo apt install curl ca-certificates gnupg

3. Add pgAdmin Repository

curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo tee /usr/share/keyrings/packages-pgadmin-org.gpg >/dev/null
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

4. Install pgAdmin

For the Web Version:

sudo apt install pgadmin4-web

OR
For the Desktop Version:

sudo apt install pgadmin4-desktop

5. Configure pgAdmin Web (Optional)

To configure the web-based pgAdmin:

sudo /usr/pgadmin4/bin/setup-web.sh

Follow the instructions to set up:

  • Email
  • Password

6. Start pgAdmin

  • Web Version: Open your browser and go to:
http://localhost/pgadmin4
  • Desktop Version:
pgadmin4

🎯 Uninstall pgAdmin

If you want to remove pgAdmin:

sudo apt remove pgadmin4
sudo apt autoremove

✅ Now pgAdmin is installed and ready to use!

Install PostgreSQL in ubuntu

To install PostgreSQL on Ubuntu, follow these steps:

✅ Steps to Install PostgreSQL on Ubuntu

  1. Update Package List
sudo apt update
sudo apt upgrade
  1. Install PostgreSQL
sudo apt install postgresql postgresql-contrib
  1. Check PostgreSQL Service Status
sudo systemctl status postgresql

If running, it will show active (running).

  1. Start/Stop/Restart Service
  • Start: sudo systemctl start postgresql
  • Stop: sudo systemctl stop postgresql
  • Restart: sudo systemctl restart postgresql

✅ Access PostgreSQL

Switch to the default PostgreSQL user:

sudo -i -u postgres

Then access the PostgreSQL prompt:

psql

You will see:

postgres=#

✅ Create a New Database

CREATE DATABASE mydb;

✅ Create a New User

CREATE USER myuser WITH PASSWORD 'mypassword';

✅ Grant Privileges

GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

✅ Exit PostgreSQL

\q

Exit user:

exit

🎯 Enable Remote Access (Optional)

  1. Edit postgresql.conf:
sudo nano /etc/postgresql/*/main/postgresql.conf

Uncomment:

listen_addresses = '*'
  1. Edit pg_hba.conf:
sudo nano /etc/postgresql/*/main/pg_hba.conf

Add:

host    all             all             0.0.0.0/0               md5
  1. Restart PostgreSQL:
sudo systemctl restart postgresql

✅ Now PostgreSQL is installed and ready to use!

Saturday, February 17, 2024

MySQL: Explain RPAD() function in MySQL.

 

In MySQL, the RPAD() function is used to right-pad a string with a specified set of characters, up to a certain length. This is often used to format strings, ensuring that they have a specific length by adding characters to the right side of the original string.

The basic syntax of the RPAD() function is as follows:

sql
RPAD(str, length, padstr)
  • str: The original string that you want to right-pad.
  • length: The length of the resulting string after padding.
  • padstr: The string or characters to use for padding. This is optional, and if not provided, it defaults to a space.

Here's an example:

sql
SELECT RPAD('123', 5, '0') AS padded_string;

This query will return the string '12300' because the original string '123' is right-padded with zeros to make the total length 5.

If the padstr parameter is not provided, it defaults to a space character. For example:

sql
SELECT RPAD('abc', 6, ' ') AS padded_string;

This query will return the string 'abc ' because the original string 'abc' is right-padded with spaces to make the total length 6.

The RPAD() function is useful in scenarios where you need to format strings to a specific length, such as when dealing with fixed-width data in a table or when creating formatted outputs.

Friday, February 16, 2024

MySQL: Explain LPAD() function in MySQL.

 

In MySQL, the LPAD() function is used to left-pad a string with a specified set of characters, up to a certain length. This is often used to format strings, ensuring that they have a specific length by adding characters to the left side of the original string.

The basic syntax of the LPAD() function is as follows:

sql
LPAD(str, length, padstr)
  • str: The original string that you want to left-pad.
  • length: The length of the resulting string after padding.
  • padstr: The string or characters to use for padding. This is optional, and if not provided, it defaults to a space.

Here's an example:

sql
SELECT LPAD('123', 5, '0') AS padded_string;

This query will return the string '00123' because the original string '123' is left-padded with zeros to make the total length 5.

If the padstr parameter is not provided, it defaults to a space character. For example:

sql
SELECT LPAD('abc', 6,' ') AS padded_string;

This query will return the string ' abc' because the original string 'abc' is left-padded with spaces to make the total length 6.

The LPAD() function is useful in scenarios where you need to format strings to a specific length, such as when dealing with fixed-width data in a table or when creating formatted outputs.

MySQL: Explain GROUP_CONCAT() function in MySQL.

 

The GROUP_CONCAT() function in MySQL is used to concatenate values from multiple rows into a single string within each group defined by the GROUP BY clause. It is often used to create a comma-separated list or concatenate strings within each group.

The basic syntax of the GROUP_CONCAT() function is as follows:

sql
GROUP_CONCAT([DISTINCT] expression [ORDER BY {column_name | expr | position} [ASC | DESC] [, ...] [SEPARATOR str_val]])
  • DISTINCT: Optional keyword to remove duplicate values before concatenation.
  • expression: The column or expression whose values you want to concatenate.
  • ORDER BY: Optional clause to specify the order in which values are concatenated within each group.
  • ASC | DESC: Optional sorting order for the ORDER BY clause.
  • SEPARATOR: Optional parameter to specify the separator between concatenated values. The default separator is a comma (,).

Here's an example to illustrate the usage of GROUP_CONCAT():

sql
SELECT department, GROUP_CONCAT(employee_name ORDER BY hire_date DESC SEPARATOR ', ') AS employee_list FROM employees GROUP BY department;

In this example:

  • The employee_name values within each department group are concatenated into a single string.
  • The concatenation is ordered by hire_date in descending order (ORDER BY hire_date DESC).
  • The values are separated by a comma and space (SEPARATOR ', ').

It's important to note that the GROUP_CONCAT() function is commonly used with the GROUP BY clause. If you use it without grouping, it will aggregate all rows into a single concatenated string.

sql
SELECT GROUP_CONCAT(employee_name ORDER BY hire_date DESC SEPARATOR ', ') AS all_employees FROM employees;

In this case, it concatenates all employee_name values into a single string for all rows in the employees table.

Be cautious about the length limitations of the result string, as there is a system variable group_concat_max_len that determines the maximum length of the result string produced by GROUP_CONCAT(). If your concatenated string exceeds this limit, you may need to adjust the variable or consider other solutions.

AHSEC| CLASS 11| GEOGRAPHY| SOLVED PAPER - 2015| H.S.1ST YEAR

  AHSEC| CLASS 11| GEOGRAPHY| SOLVED PAPER - 2015| H.S.1ST YEAR 2015 GEOGRAPHY SOLVED PAPER Full Marks: 70 Time: 3 hours The figures in the...