Database Archives - TEKSpace Blog https://blog.tekspace.io/tag/database/ Tech tutorials for Linux, Kubernetes, PowerShell, and Azure Mon, 16 Oct 2023 18:21:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://blog.tekspace.io/wp-content/uploads/2023/09/cropped-Tekspace-logo-icon-32x32.png Database Archives - TEKSpace Blog https://blog.tekspace.io/tag/database/ 32 32 How To Backup And Restore PostgreSQL Database https://blog.tekspace.io/how-to-backup-and-restore-postgresql-database/ https://blog.tekspace.io/how-to-backup-and-restore-postgresql-database/#respond Mon, 16 Oct 2023 17:01:17 +0000 https://blog.tekspace.io/?p=1796 In this tutorial, I will share how to export a database using postgres user. To export entire database into a file, execute the below command from the terminal. Backup PostgreSQL Database Here are the options I used to export a database to a file. Restore PostgreSQL Database Now connect to your new server and execute

The post How To Backup And Restore PostgreSQL Database appeared first on TEKSpace Blog.

]]>
In this tutorial, I will share how to export a database using postgres user.

To export entire database into a file, execute the below command from the terminal.

Backup PostgreSQL Database

pg_dump -d tekspace_db_dev -U postgres -h localhost > tekspace_dev.dump.txt

Here are the options I used to export a database to a file.

Connection options:
  -d, --dbname=DBNAME      database to dump
  -h, --host=HOSTNAME      database server host or socket directory
  -p, --port=PORT          database server port number
  -U, --username=NAME      connect as specified database user
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)
  --role=ROLENAME          do SET ROLE before dump

Restore PostgreSQL Database

Now connect to your new server and execute the below command and replace [database_name].

psql -h localhost -U postgres -W [database_name] < tekspace_dev.dump.txt

Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "/var/run/postgresql")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "rahil")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)

That’s all!

The post How To Backup And Restore PostgreSQL Database appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-backup-and-restore-postgresql-database/feed/ 0
How To Install PostgreSQL On Ubuntu Linux https://blog.tekspace.io/how-to-install-postgresql-on-ubuntu-linux/ https://blog.tekspace.io/how-to-install-postgresql-on-ubuntu-linux/#respond Sat, 07 Oct 2023 17:08:30 +0000 https://blog.tekspace.io/?p=1770 Postgres, also known as PostgreSQL, is a system for managing databases that uses the SQL language. It follows standard rules and offers advanced options like reliable transactions and the ability to handle multiple tasks simultaneously without causing reading delays. The tutorial explains how to set up Postgres on an Ubuntu 20.04 server. It covers the

The post How To Install PostgreSQL On Ubuntu Linux appeared first on TEKSpace Blog.

]]>
Postgres, also known as PostgreSQL, is a system for managing databases that uses the SQL language. It follows standard rules and offers advanced options like reliable transactions and the ability to handle multiple tasks simultaneously without causing reading delays.

The tutorial explains how to set up Postgres on an Ubuntu 20.04 server. It covers the installation process and includes instructions on creating a new user and database.

This tutorial focuses on PostgreSQL version 16.0.

Prerequisites

Step 1: Install PostgreSQL

Enable PostgreSQL APT Respository.

sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo apt update -y && sudo apt upgrade -y

We will now install PostgreSQL packages.

sudo apt install postgresql postgresql-contrib -y

Next, we will start the PostgreSQL service and enable it to start the service on reboot.

sudo systemctl start postgresql.service && sudo systemctl enable postgresql.service

Step 2: PostgreSQL Roles and Databases

Postgres, by default, employs a mechanism termed “roles” for both authentication and authorization tasks. This is somewhat akin to the typical Unix-style user and group system.

When you install Postgres, it’s configured to use ident authentication. This means it links Postgres roles to corresponding Unix/Linux system accounts. If a role is present in Postgres, a Unix/Linux user with an identical name can log in under that role.

During the installation, a user named Postgres is established, which corresponds to the primary Postgres role. There are multiple methods to access Postgres using this account. One method is to transition to the Postgres account on your server using a specific command.

sudo -i -u postgres

Now, you can access the Postgres prompt by running the below command:

psql

This action will grant you access to the PostgreSQL interface, allowing you to immediately engage with the database management system.

To exit from the PostgreSQL interface, execute the subsequent command:

\q

Once you have exited the PostgreSQL prompt, you can easily switch back to your logged-in user from the Postgres user.

exit

Alternative way to connect to Postgres prompt:

sudo -i -u postgres psql

Step 3: Create A New Role in PostgreSQL

Once you are logged in as Postgres user, you can enter the below command:

createuser --interactive

Alternatively, you can also use the below command to run as sudo user.

sudo -u postgres createuser --interactive

output:

postgres@tekspace-db-dev:~$ createuser --interactive
Enter name of role to add: tekspace-db-dev
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

Step 4: Create A New Database in PostgreSQL

When Postgres sets things up, it expects that each role (or user) will have a database with the same name they can use.

So, if you made a user named “tekspace-db-dev” before, it will try to connect to a database named “tekspace-demo” automatically. To make this database, you can use the “createdb” command.

If you’re using the Postgres account, you’d type a command similar to this:

createdb tekspace-db-dev

Alternatively, you can also run this with sudo user:

sudo -u postgres createdb tekspace-db-dev

Exit out of Postgres user:

exit

Step 5: Open a Postgres Prompt with A New Role

To sign in using ident authentication, your Linux username should match your Postgres role and database name.

If you don’t have a Linux username like that, you can make one using the “adduser” command. Perform this action with an account that has the necessary privileges, not the main ‘Postgres’ account. Please log in using a different account called ‘sudo’ instead of the main ‘Postgres’ account.

sudo adduser tekspace-db-dev

Output:

rahil@tekspace-db-dev:~$ sudo adduser tekspace-db-dev
[sudo] password for rahil:
Adding user `tekspace-db-dev' ...
Adding new group `tekspace-db-dev' (1001) ...
Adding new user `tekspace-db-dev' (1001) with group `tekspace-db-dev (1001)' ...
Creating home directory `/home/tekspace-db-dev' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for tekspace-db-dev
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] Y
Adding new user `tekspace-db-dev' to supplemental / extra groups `users' ...
Adding user `tekspace-db-dev' to group `users' ...

After a login user has been created, now you can navigate to Postgres prompt by executing the below command:

sudo -i -u tekspace-db-dev

To access the prompt in PostgreSQL as a newly created user, execute the following command after logging in:

psql

If you want to connect to a different database, you can specify the database as shown below:

psql -d postgres

Output:

tekspace-db-dev@tekspace-db-dev:~$ psql -d postgres
psql (15.4 (Ubuntu 15.4-0ubuntu0.23.04.1))
Type "help" for help.

Once you are logged-in, you can check your current connection information by executing the below command:

\conninfo

postgres=> \conninfo
You are connected to database "postgres" as user "tekspace-db-dev" via socket in "/var/run/postgresql" at port "5432".
postgres=>

That’s it! You have successfully set up a PostgreSQL server on an Ubuntu Linux server.

User & Database Management

CREATE DATABASE tekspace_db_dev;
CREATE USER tekspace_db_dev_user WITH PASSWORD 'super_secret_password';
GRANT ALL PRIVILEGES ON DATABASE tekspace_db_dev to tekspace_db_dev_user;

Next connect with admin account to a newly created database:

psql -h localhost -U postgres -d tekspace_db_dev -W

This is a workaround for PostgreSQL 15.

GRANT ALL ON SCHEMA public TO tekspace_db_dev_user;

Now connect to a newly created database with its user:

psql -h localhost -U tekspace_db_dev_user -d tekspace_db_dev -W

Change Password For Postgres USER

To change the password for Postgres user follow below steps.

sudo -i -u postgres

psql

ALTER USER postgres PASSWORD 'Super secret password';

The post How To Install PostgreSQL On Ubuntu Linux appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-install-postgresql-on-ubuntu-linux/feed/ 0
How to install MySQL 8 on CentOS 8 https://blog.tekspace.io/how-to-install-mysql-8-on-centos-8/ https://blog.tekspace.io/how-to-install-mysql-8-on-centos-8/#respond Sat, 19 Sep 2020 16:58:02 +0000 https://blog.tekspace.io/index.php/2020/09/19/how-to-install-mysql-8-on-centos-8/ Download rpm package and configure yum repository Disable mysql5.7: Enable mysql8.0: Verify: Disabling default MySQL modules Installation Service start stop start: status check: Changing temp password after installation The above output will display the default password Z#5m,9wsmlw in the console. Changing password by following below setup command Interactive setup: Connecting to MySQL server

The post How to install MySQL 8 on CentOS 8 appeared first on TEKSpace Blog.

]]>
Download rpm package and configure yum repository
wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
sudo yum localinstall mysql80-community-release-el8-1.noarch.rpm
sudo yum repolist enabled | grep "mysql.*-community.*"

output:
mysql-connectors-community              MySQL Connectors Community
mysql-tools-community                   MySQL Tools Community
mysql80-community                       MySQL 8.0 Community Server
sudo yum repolist all | grep mysql

Disable mysql5.7:

sudo yum-config-manager --disable mysql57-community

Enable mysql8.0:

sudo yum-config-manager --enable mysql80-community

Verify:

sudo yum repolist enabled | grep mysql

Disabling default MySQL modules

sudo yum module disable mysql

Installation

sudo yum install mysql-community-server

Service start stop

start:

sudo service mysqld start

status check:

sudo service mysqld status

Changing temp password after installation

sudo grep 'temporary password' /var/log/mysqld.log

output:
2020-09-19T16:33:12.280169Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: .Z#5m,9wsmlw

The above output will display the default password Z#5m,9wsmlw in the console.

Changing password by following below setup command

sudo mysql_secure_installation

Interactive setup:

Securing the MySQL server deployment.

Enter password for user root: Enter New Root Password

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: Set New MySQL Password

Re-enter new password: Re-enter New MySQL Password

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

Connecting to MySQL server

mysql -u root -p

The post How to install MySQL 8 on CentOS 8 appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-install-mysql-8-on-centos-8/feed/ 0