Recent research shows that most (64%) companies worldwide deal with over 1,000 terabytes (TB) of data. Without proper management, this can present several problems, ranging from security vulnerabilities to operational inefficiencies and accessibility issues.
This makes creating and setting up a database essential. PostgreSQL is one solution to create an efficient data management system. Known for its advanced and rich features, scalability, reliability, and performance, PostgreSQL stands out as one of the best database systems.
There are several requirements:
If you don’t already have PostgreSQL installed on your operating system or server instance, you can download it as ready-to-use packages or source code archives.
Using package managers is often the simplest approach, as they automatically resolve dependencies and streamline the installation process. For Ubuntu and other Debian-based systems, you can use apt:
sudo apt update
sudo apt install postgresql
On macOS, you can use Homebrew:
brew install postgresql
Once you install it, verify that it’s correctly set up by opening your command prompt or terminal and run the command “psql --version.” This command only checks if the client is installed. It does not verify the PostgreSQL server setup.
If you want to manage PostgreSQL as a service, you may need to use system-specific commands.
For instance, if you’re using Linux:
sudo systemctl start postgresql
sudo systemctl stop postgresql
If using Windows, open the Services panel, find PostgreSQL, and use the start/stop options.
You can connect to PostgreSQL in two ways:
Open a terminal on Unix systems or a command prompt on Windows.
Enter the “psql -U postgres” command to connect to PostgreSQL.
“-U postgres” specifies the username, which by default is usually “postgres” during installation.
If you created a custom user, replace “postgres” with that name.
You’ll see a prompt asking for your password, as shown below:
password for user postgres
From the Start menu on Windows, launch the pgAdmin application.
Note: Ensure the server is running and accessible before attempting to connect.
On the pgAdmin dashboard, right-click the server name and select “Connect.”
If you don’t have a server, you can create one by selecting “Create” and then “Server.”
Provide a name for the server connection, such as “Local PostgreSQL.”
On the connection tab, enter the details as follows:
Click “Save” to add the server to pgAdmin.
This method involves running the following syntax:
CREATE DATABASE database_name;
Should you need to customize your database specifics, you can include additional parameters in the basic syntax, such as:
pgAdmin lets you create databases in a simplified manner by leveraging a graphical interface. It involves the following steps:
If you want to list all the databases on your server or find a specific one, you can use the \l command or \l+ for detailed information, such as database sizes, descriptions, and locale settings.
You can also verify the owner of each database by running:
SELECT datname, datowner FROM pg_database;
Lastly, if you want to give a user access to a specific database, you can run:
GRANT ALL PRIVILEGES ON DATABASE … TO …
Note: This gives full control to the user. We recommend using least privilege principles, such as granting only required privileges as shown below:
GRANT CONNECT ON DATABASE delinea TO delineauser;
This grants basic connection rights but not the ability to modify the database.
You may encounter some common issues and errors when creating databases in PostgreSQL:
This may occur if your user role doesn’t have CREATEDB privileges or superuser permissions.
You can rectify this by having a superuser run:
ALTER USER your_username CREATEDB;
This issue will occur if you name a new database with an existing name on the server.
If unsure about a name, you can always use the \l command to list all the existing databases.
Alternatively, you can run the following SQL query:
SELECT datname FROM pg_database WHERE datname = 'database_name';
Once a database is created, you can’t modify the encoding and locale settings. The only solution is to recreate the database with the desired settings. As such, ensure you specify these settings when creating the database.
To check the current encoding and locale settings of your database during creation, use:
SHOW lc_collate;
SHOW lc_ctype;
SHOW client_encoding;
If you frequently need to create databases, such as during continuous integration testing, you may need to integrate automation to expedite processes and save time.
In such a case, you can leverage scripting via the CREATEDB command.
Note: CREATEDB is a shell utility and may not be available in all environments unless the PostgreSQL client tools are installed.
You can also leverage templates to create new databases with specific structures.
For example, the command below uses custom template0 to create database:
CREATE DATABASE delinea TEMPLATE template0;
Delinea provides a centralized dashboard that lets you manage permissions, log actions through audit trails, and easily integrate with whatever tool you use when creating databases. Additional features that our solution provides include:
Ready to experience the power of Delinea? Start a free trial today.