How to Add a Database in PostgreSQL Using SQL and pgAdmin Tools?

Adding a new database in PostgreSQL helps keep projects separate and secure. This guide shows step-by-step how to create databases with SQL commands or the pgAdmin tool, plus tips for setup and troubleshooting.

download-icon
Free Download
for VM, OS, DB, File, NAS, etc.
dan-zeng

Updated by Dan Zeng on 2025/10/10

Table of contents
  • What Is Adding a Database in PostgreSQL?

  • Prerequisites for Creating a New Database

  • Method 1: Using SQL Command Line

  • Method 2: Using pgAdmin Tool

  • How Vinchin Backup & Recovery Protects Your PostgreSQL Databases

  • postgresql add database FAQs

  • Conclusion

Adding a database in PostgreSQL is a core skill for any operations administrator. Each database acts as an isolated container—separating data by project or client. This isolation improves security boundaries and resource allocation. If you manage multiple applications or environments on one server, knowing how to create databases efficiently helps you avoid conflicts and streamline workflows.

This guide covers everything from basic setup to advanced configuration. We’ll start with what it means to add a database in PostgreSQL, review prerequisites, then walk through two proven methods: using the SQL command line and pgAdmin graphical tool. You’ll also learn about advanced options, troubleshooting common errors, protecting your data after creation—and get answers to practical questions.

What Is Adding a Database in PostgreSQL?

In PostgreSQL, adding a database means creating an independent storage area within your server instance. Each database has its own set of tables, schemas, roles (users), permissions, and settings. This separation lets you manage different projects without overlap or risk of accidental data mixing.

When you create a new database in PostgreSQL, it copies all objects from a template—usually template1. This ensures every new database starts with standard system catalogs and configurations unless you specify otherwise. The process is fast because it uses file-level copying rather than building structures from scratch.

Prerequisites for Creating a New Database

Before you can add a new database in PostgreSQL, make sure these conditions are met:

First, confirm that your PostgreSQL server is running—either locally or remotely—and that you can connect using your credentials. Only superusers or users granted the CREATEDB privilege can create databases.

To check if your account has this right:

1. Connect using psql.

2. Run \du to list roles; look for Create DB next to your username.

If not present, ask an administrator for access or use a superuser account.

Also verify that there is enough disk space available on the server’s storage volume; large databases need plenty of free space both during creation and future growth. For high-load environments or production systems handling many connections or transactions per second (TPS), ensure adequate memory is allocated at the system level.

If you plan to use pgAdmin as your interface:

  • Install pgAdmin on your workstation or access it via browser if hosted centrally.

  • Configure it so it can reach your target server over the network.

Method 1: Using SQL Command Line

The SQL command line gives you direct control over every aspect of adding databases in PostgreSQL.

Start by connecting via psql. If local peer authentication is enabled (the default on many Linux systems), you may need to switch users:

sudo -i -u postgres
psql

Or connect directly as yourself:

psql -U your_username -d postgres

Once connected:

1. To create a simple new database named mydb, enter:

   CREATE DATABASE mydb;

2. To customize ownership or encoding—for example if another team member should own it—use:

   CREATE DATABASE mydb
     WITH OWNER = other_user
     ENCODING = 'UTF8'
     CONNECTION LIMIT = 20;

3. For custom locale or collation settings (important for sorting rules):

   CREATE DATABASE mydb
     WITH LC_COLLATE = 'en_US.UTF-8'
     LC_CTYPE = 'en_US.UTF-8';

4. To specify where files are stored physically (advanced storage management), set a custom tablespace:

   CREATE DATABASE mydb TABLESPACE fast_ssd_space;

5. If you want full control over initial state—such as avoiding inherited settings from template1—create from scratch using:

   CREATE DATABASE mydb TEMPLATE template0;

After running any of these commands successfully:

  • See all databases by typing \l

  • Connect to your new one with \c mydb

Alternatively, from your operating system shell prompt outside psql, run:

createdb -U your_username mydb

This does the same thing as above but uses system-level tools instead of SQL statements inside psql.

Remember: Always validate that you have permission before executing these commands; otherwise you'll see “permission denied” errors.

Method 2: Using pgAdmin Tool

pgAdmin is PostgreSQL’s official graphical interface—a good choice if you prefer visual workflows over typing commands.

Begin by launching pgAdmin and connecting it to your target server instance using valid credentials.

In the left sidebar tree view under Servers, expand until you see Databases beneath your chosen server name.

To add a new database:

1. Right-click Databases

2. Select Create, then click Database…

3. In the dialog box that appears under General, type the desired name (e.g., mydb)

4. Choose an owner from the dropdown list under Owner

5. Optionally click into tabs like Definition or Security to set encoding (UTF8 is standard), select templates (template0 for minimal setup), assign tablespaces for performance tuning—or limit allowed connections

6. Before saving changes, open the SQL tab at top right; this shows exactly what SQL will be executed behind-the-scenes—a great way to learn syntax!

7. Click Save

If you don’t see your new database immediately in the object browser pane after saving—right-click anywhere inside that pane and choose Refresh.

You can now expand this entry further within pgAdmin’s tree view structure—to add schemas/tables/roles—or manage permissions visually without writing code.

How Vinchin Backup & Recovery Protects Your PostgreSQL Databases

After successfully adding new databases in PostgreSQL, implementing robust protection becomes essential for business continuity and compliance needs alike. Vinchin Backup & Recovery stands out as an enterprise-grade solution supporting most mainstream platforms—including first-class support for PostgreSQL alongside Oracle, MySQL, SQL Server, MariaDB, PostgresPro, and MongoDB environments.

Vinchin Backup & Recovery delivers critical features such as incremental backup for efficient data capture between runs; batch backup management across multiple instances; flexible data retention policies including GFS retention schemes; comprehensive ransomware protection mechanisms; and seamless restore-to-new-server capabilities—all designed to maximize reliability while minimizing administrative overhead and storage costs.

The intuitive web console streamlines backup operations into four straightforward steps tailored specifically for PostgreSQL:

1.     Go to Database Backup> Backup, and select the data source from the licensed database list.

a2ba4a3638798e05832c9ccfb78ffd7.png

2.     Select the target node and storage for the job.

bdbae7ff43eba973ae444ae8f2058ac.png

3.     Set up desired backup strategies.

318e2800f93d4dabe5bbcf53e04e5ad.png

3a1120d880a242c1985bc9c81c2ff93.png

54895188fcca4dcd75171e61e98c5cf.png

9ccb1ed77a56107bd1dcfc5d5d47ac7.png

4.     View settings and click Submit.

Trusted worldwide by thousands of organizations with top-rated customer satisfaction scores—Vinchin Backup & Recovery offers a fully featured 60-day free trial so IT teams everywhere can experience secure enterprise data protection firsthand! Click below to get started today.

postgresql add database FAQs

Q1: How do I automatically grant default privileges on objects created in my new database?

A1: After connecting as owner run ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO reporting_role;

Q2: Are there differences between Linux and Windows when naming databases?

A2: Yes—Linux treats names case-sensitively while Windows does not; always use lowercase names unless quoting identifiers everywhere consistently.

Q3: Can I script both schema creation and initial table population right after making my new DB?

A3: Yes—connect using psql -d dbname -f init_script.sql immediately following CREATE DATABASE execution.

Conclusion

Adding databases in PostgreSQL boosts flexibility across projects while keeping data secure through strong isolation features—and both command line plus GUI methods work well depending on preference! Always validate configurations thoroughly before production use; protect every environment reliably with Vinchin backup integration too.

Share on:

Categories: Database Backup