Install PostgreSQL on Ubuntu 20.04
Install PostgreSQL on Ubuntu 20.04
To export a PostgreSQL database schema and data, one typically uses the pg_dump tool. For example, the command: $ pg_dump -U postgres -d database_name -F c -f backup_file.dump exports the specified database to a custom-format archive file. This file can be restored with pg_restore, providing flexibility in database backup and restoration strategies .
After PostgreSQL installation, connect to the database server by switching to the postgres account with: $ sudo -i -u postgres. Then start the psql tool by typing: $ psql. This will bring up the PostgreSQL prompt, where database queries and commands can be executed .
PostgreSQL uses ident authentication, which links PostgreSQL roles to system user accounts. This means that if a PostgreSQL role exists with the same name as a system user account, that account can log in as the PostgreSQL role without needing a password. This is significant for authentication because it simplifies the login process and ties database access permissions directly to system-level user management, enhancing integration and security .
Installing PostgreSQL via package management on Linux is recommended because it ensures proper integration with the operating system, including functionalities like automatic patching and update management. This approach can reduce maintenance load and improve system stability since the installation and updates are handled within the system's native package management workflow .
After working with the PostgreSQL administrative account, you can switch back to a regular system user by executing the 'exit' command at the Linux command prompt. This will return the shell session from the postgres account back to the originating system user account .
To install PostgreSQL on Ubuntu 20.04, first, create the file repository configuration using the command: $ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'. Second, import the repository signing key: $ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -. Third, update the package list: $ sudo apt-get update. Finally, install the latest version of PostgreSQL with: $ sudo apt-get install postgresql. Alternatively, to install a specific version, use $ sudo apt-get install postgresql-version, replacing 'version' with the specific version number .
'Ident authentication' benefits PostgreSQL by linking database roles to existing system user accounts, which simplifies authentication by eliminating the need for separate credentials. It improves security by utilizing existing operating system controls and reduces administration overhead as user management is handled centrally at the system level .
The 'pg_restore' command in PostgreSQL is used to restore a PostgreSQL database from an archive created by pg_dump or similar tools. It is particularly useful for restoring complex databases that may include multiple schemas or tables, as it can handle a variety of file formats and allows for the selective restoration of objects from the archive .
To install a specific version of PostgreSQL on Ubuntu, after adding the PostgreSQL repository, use the command: $ sudo apt-get install postgresql-version, replacing 'version' with the desired version number (e.g., postgresql-12 for version 12). This ensures precision in deployment when specific features or compatibility are required .
To load a sample database, first switch to the postgres account: $ sudo -i -u postgres. Download the sample database using curl: $ curl -O https://sp.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip. Unzip the file: $ unzip dvdrental.zip. Access PostgreSQL with the psql tool: $ psql. Create the database using the command: postgres=# create database dvdrental;. Quit psql: \. Use pg_restore to restore the database: $ pg_restore --dbname=dvdrental --verbose dvdrental.tar. Reconnect with psql: $ psql, and switch to the new database with: postgres=# \c dvdrental. Verify by executing: dvdrental=# select count(*) from film;, to see an output indicating 1000 films .