PostgreSQL Archives - TEKSpace Blog https://blog.tekspace.io/category/database/postgresql/ Tech tutorials for Linux, Kubernetes, PowerShell, and Azure Thu, 01 Feb 2024 20:13:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.7 https://blog.tekspace.io/wp-content/uploads/2023/09/cropped-Tekspace-logo-icon-32x32.png PostgreSQL Archives - TEKSpace Blog https://blog.tekspace.io/category/database/postgresql/ 32 32 Importing and Exporting Data in PostgreSQL https://blog.tekspace.io/importing-and-exporting-data-in-postgresql/ https://blog.tekspace.io/importing-and-exporting-data-in-postgresql/#respond Fri, 12 Jan 2024 16:53:49 +0000 https://blog.tekspace.io/?p=1810 Introduction Effectively managing data is a key aspect of database administration. In PostgreSQL, scenarios often arise where data needs to be exported from one database and imported into another. This post will detail a practical approach to exporting data from a PostgreSQL table and then importing it into another database using command-line tools. Exporting Data […]

The post Importing and Exporting Data in PostgreSQL appeared first on TEKSpace Blog.

]]>
Introduction

Effectively managing data is a key aspect of database administration. In PostgreSQL, scenarios often arise where data needs to be exported from one database and imported into another. This post will detail a practical approach to exporting data from a PostgreSQL table and then importing it into another database using command-line tools.

Exporting Data from PostgreSQL

Scenario

You want to export data from a specific table, named Application, in a case-sensitive manner from a PostgreSQL database.

Solution: Using pg_dump

pg_dump is a utility provided by PostgreSQL for backing up a database. It can export data in a format suitable for later restoration or transfer to another database.

Command:
pg_dump -U acme_db_sandbox_user -h localhost -d acme_db_sandbox -t '"Application"' --column-inserts --data-only > output.sql
Explanation:
  • -U acme_db_sandbox_user: Specifies the user for database connection.
  • -h localhost: Targets the local machine for the database server.
  • -d acme_db_sandbox: Specifies the database from which to export.
  • -t '"Application"': Indicates the case-sensitive name of the table to export.
  • --column-inserts: Ensures the generation of INSERT statements with column names.
  • --data-only: Excludes database schema (structure) from the export, focusing only on the data.
  • > output.sql: Redirects the exported data to output.sql.

This command generates an SQL file (output.sql) containing INSERT statements for each row in the Application table.

Importing Data into PostgreSQL

Scenario

Now, you need to import the previously exported data into a different database.

Solution: Using psql

psql is a command-line interface used to interact with PostgreSQL. It allows you to execute SQL queries and run scripts.

Command:
psql -h localhost -U acme_billing_db_sandbox_user -d acme_billing_db_sandbox -W -f output.sql
Explanation:
  • -h localhost: Connects to the PostgreSQL server on the local machine.
  • -U acme_billing_db_sandbox_user: Specifies the user for the target database.
  • -d acme_billing_db_sandbox: Designates the target database.
  • -W: Prompts for the user’s password as a security measure.
  • -f output.sql: Executes the SQL commands in output.sql.

This command imports the data from output.sql into the specified database. Ensure that the target database has the appropriate table structure to accommodate the data.

Conclusion

Transferring data between PostgreSQL databases can be efficiently handled using command-line tools like pg_dump and psql. This process involves exporting data in SQL format using pg_dump and then importing it with psql. This approach is particularly useful for database administrators and developers looking to manage data across different environments or systems.

Remember, accurate knowledge of your database schema and careful command execution are key to successful data transfer. Happy database managing!

The post Importing and Exporting Data in PostgreSQL appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/importing-and-exporting-data-in-postgresql/feed/ 0
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 Allow PostgreSQL Connection From Remote IP https://blog.tekspace.io/how-to-allow-postgresql-connection-from-remote-ip/ https://blog.tekspace.io/how-to-allow-postgresql-connection-from-remote-ip/#respond Sat, 07 Oct 2023 23:36:19 +0000 https://blog.tekspace.io/?p=1780 In the last line, add the following details: Open postgresql.conf Find #listen_addresses = 'localhost', then uncomment and replace with the following values: listen_addresses = '*' and save the file. What this does is, it opens connection for all IP range inside 10.116.0.0/20 subnet only. Next, let’s restart PostgreSQL service.

The post How To Allow PostgreSQL Connection From Remote IP appeared first on TEKSpace Blog.

]]>

sudo vi /etc/postgresql/15/main/pg_hba.conf

In the last line, add the following details:

host    all             all             10.116.0.0/20           scram-sha-256

Open postgresql.conf

sudo vi /etc/postgresql/15/main/postgresql.conf

Find #listen_addresses = 'localhost', then uncomment and replace with the following values: listen_addresses = '*' and save the file.

What this does is, it opens connection for all IP range inside 10.116.0.0/20 subnet only.

sudo systemctl restart postgresql.service

Next, let’s restart PostgreSQL service.

The post How To Allow PostgreSQL Connection From Remote IP appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-allow-postgresql-connection-from-remote-ip/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