Here’s a detailed guide on how to download, set up, and use PostgreSQL on Linux Manjaro, structured for use as a concise and practical cheatsheet.
PostgreSQL on Manjaro: Installation and Setup Guide
1. Install PostgreSQL
Manjaro uses pacman
as its package manager, and PostgreSQL is available in the official repositories.
2. Initialize the Database Cluster
PostgreSQL requires a data directory for storing databases. After installation, you need to initialize this directory.
--locale=en_US.UTF-8
ensures UTF-8 encoding for text storage.-D
specifies the data directory.
3. Start and Enable PostgreSQL Service
To use PostgreSQL, start the database server and enable it to start on boot.
4. Switch to the PostgreSQL User
PostgreSQL uses a special system user (postgres
) for managing the database.
Once logged in as postgres
, you can use the psql
command-line utility.
5. Create a New Role and Database
By default, PostgreSQL has a postgres
superuser role. To create your user and database:
6. Connect to Your Database
Exit from the postgres
user and log in as your Linux user. You can then connect to your database:
-U
: Specifies the username.-d
: Specifies the database name.-h
: Specifies the host (localhost).
7. Basic PostgreSQL Commands
Working with Databases
- List all databases:
- Connect to a database:
- Create a database:
- Drop a database:
Working with Tables
- List tables:
- Create a table:
- Insert data:
- Query data:
- Drop a table:
Miscellaneous
- Quit
psql
: - Show help for SQL commands:
8. Enable Remote Connections (Optional)
To allow remote connections:
-
Edit the PostgreSQL configuration file:
- Uncomment and set
listen_addresses
:
- Uncomment and set
-
Configure access control:
Edit thepg_hba.conf
file:Add a line like this to allow connections:
-
Restart PostgreSQL:
9. Backup and Restore
Backup a database:
Restore a database:
10. Uninstall PostgreSQL (if needed)
To remove PostgreSQL:
Additional Tips
- Logs: PostgreSQL logs are located at
/var/lib/postgres/data/log
. - Documentation: Use
man psql
or visit the official PostgreSQL documentation.
This guide is designed to give you a quick start and reference for PostgreSQL on Manjaro. Save it for troubleshooting, setups, or daily use!