fbpx

How to Start and Stop the PostgreSQL Database

AWS

				
					aws rds start-db-instance --db-instance-identifier <instance_name>
				
			
				
					aws rds stop-db-instance --db-instance-identifier <instance_name>

				
			

Linux

				
					--Run as postgresql linux user
pg_ctl start -D <data_directory>

				
			
				
					--Run as postgresql linux user
pg_ctl stop -D <data_directory>

				
			
				
					sudo systemctl start postgresql

				
			
				
					sudo systemctl stop postgresql

				
			

Azure

				
					az login
az postgres server stop --resource-group <resource-group-name> --name <server-name>

				
			
				
					az postgres server start --resource-group <resource-group-name> --name <server-name>

				
			

awscli Command to Find PostgreSQL rds Free and Used Space

To determine the free and used disk space of a PostgreSQL RDS instance using the AWS CLI, you can use the describe-db-instances command and filter the output to show the FreeStorageSpace and AllocatedStorage fields:

				
					aws rds describe-db-instances --query 'DBInstances[*].{ID:DBInstanceIdentifier, Free:FreeStorageSpace, Allocated:AllocatedStorage}' --output table

				
			

This will give you a table with the ID of the RDS instance, the Free disk space in GB, and the Allocated disk space in GB. You can use these values to calculate the used disk space by subtracting the free disk space from the allocated disk space.

How to Resize PostgreSQL rds Disk Size Using awscli commands

To resize the disk size of a PostgreSQL RDS instance using the AWS CLI, you need to follow these steps but before that try to get the used and free space by running these commands from my artical

1.First, stop the RDS instance.

				
					aws rds stop-db-instance --db-instance-identifier <instance_name>

				
			

Modify the RDS instance’s disk size using the modify-db-instance command:

				
					aws rds modify-db-instance --db-instance-identifier <instance_name> --allocated-storage <new_size_in_gb>

				
			

start the instance

				
					aws rds start-db-instance --db-instance-identifier <instance_name>

				
			
Note: The size change will only occur when you stop and start the instance. Also, back up your data before performing any disk size changes.

Copy Table From one PostgreSQL RDS to Another using Python Boto3

To copy a table from one PostgreSQL RDS instance to another using Python and boto3, you will need to:

  1. Install the Python library psycopg2, which is a PostgreSQL adapter for Python. You can do this by running pip install psycopg2 in your terminal.

  2. Import the necessary modules:​

				
					import boto3
import psycopg2

				
			
  1. Connect to both the source and target RDS instances using the psycopg2 library. You will need to specify the host, port, database name, username, and password for each RDS instance.

  2. Once you have established connections to both RDS instances, you can use the psycopg2 library to create a cursor object for each connection. A cursor allows you to execute PostgreSQL commands and retrieve data.

  3. Use the cursor to execute a SELECT statement on the source table. This will retrieve all of the rows from the table.

  4. Iterate over the rows returned by the SELECT statement, and use the cursor for the target RDS instance to execute an INSERT statement for each row. This will copy the data from the source table to the target table.

Here is some sample code that demonstrates how to do this:

				
					# Connect to the source RDS instance
conn1 = psycopg2.connect(host="source-rds-instance.abc123.us-east-1.rds.amazonaws.com", port=5432, database="mydatabase", user="myuser", password="mypassword")
cur1 = conn1.cursor()

# Connect to the target RDS instance
conn2 = psycopg2.connect(host="target-rds-instance.abc123.us-east-1.rds.amazonaws.com", port=5432, database="mydatabase", user="myuser", password="mypassword")
cur2 = conn2.cursor()

# Select all rows from the source table
cur1.execute("SELECT * FROM mytable")

# Iterate over the rows and insert them into the target table
for row in cur1:
    cur2.execute("INSERT INTO mytable VALUES (%s, %s, %s)", row)

# Commit the changes to the target RDS instance
conn2.commit()

# Close the cursors and connections
cur1.close()
conn1.close()
cur2.close()
conn2.close()

				
			

I hope this helps! Let me know if you have any questions.

How to ssh a Remote Linux Server?

To connect to a remote Linux server using Secure Shell (SSH), you will need to do the following:

  1. Install an SSH client on your local machine. If you are using a Unix-like operating system (e.g., Linux, macOS), you can use the built-in ssh command. If you are using Windows, you can use a program like PuTTY or MobaXterm.

  2. Determine the hostname or IP address of the remote server. This is the address that you will use to connect to the server. You can usually find the hostname or IP address in the server’s documentation or by contacting the server’s administrator.

  3. Open a terminal window on your local machine and enter the following command:

				
					ssh username@hostname

				
			

Replace username with your username on the remote server and hostname with the hostname or IP address of the server. For example:

				
					ssh john@server.example.com

				
			
  1. If this is the first time you have connected to the server, you may see a message asking you to confirm the authenticity of the server’s host key. Type yes and press Enter to continue.

  2. Enter your password when prompted. If you have entered the correct username and password, you should be logged into the remote server.

I hope this helps! Let me know if you have any questions.

Create AWS EC2 From AWS CLI with Public IP

This article will explain creating an ec2 instance with the AWS CLI option from the command line interface. AWS CLI is an easy and fast option for creating single or multiple ec2 instances. Using AWS CLI, you can automate total AWS infrastructure build and also, and you can query the existing infrastructure.

Create EC2

				
					aws ec2 run-instances --image-id  ami-090fa75af13c156b4 --count 1 --instance-type t2.micro --key-name awscli --security-group-ids sg-8f6dc695 --subnet-id subnet-88e3ea86
				
			

Create EC2 with Public IP

				
					aws ec2 run-instances --image-id  ami-090fa75af13c156b4 --count 5 --instance-type t2.micro --key-name awscli --security-group-ids sg-8f6dc695 --subnet-id subnet-88e3ea86 --associate-public-ip-address
				
			

List Runing EC2 instances

				
					aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicDNS:PublicDnsName,PublicIP:PublicIpAddress,Name:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --output table