{"id":172,"date":"2025-05-13T17:35:33","date_gmt":"2025-05-13T17:35:33","guid":{"rendered":"https:\/\/kingstatue.com\/?p=172"},"modified":"2025-05-13T17:35:33","modified_gmt":"2025-05-13T17:35:33","slug":"how-to-use-postgresql-with-django-on-windows","status":"publish","type":"post","link":"https:\/\/www.kingstatue.com\/?p=172","title":{"rendered":"How To Use PostgreSQL with Django on Windows"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Step 1: Install PostgreSQL on Windows<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Download the installer from:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.postgresql.org\/download\/windows\">https:\/\/www.postgresql.org\/download\/windows<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the installer and:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Choose a password for the <strong>postgres<\/strong> user (save it).<\/li>\n\n\n\n<li>Install pgAdmin if you want a GUI.<\/li>\n\n\n\n<li>Leave default port <strong>5432<\/strong>.<\/li>\n\n\n\n<li>Complete the installation.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">During the installation of PostgreSQL on Windows, you are asked to select components like:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>PostgreSQL Server<\/strong><\/li>\n\n\n\n<li><strong>pgAdmin 4<\/strong><\/li>\n\n\n\n<li><strong>Stack Builder<\/strong><\/li>\n\n\n\n<li><strong>Command Line Tools<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>PostgreSQL Server<\/strong> (Essential)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The actual <strong>database server<\/strong> that stores and manages your databases.<\/li>\n\n\n\n<li> It&#8217;s the core service\u2014<strong>without this, PostgreSQL won&#8217;t run<\/strong>.<\/li>\n\n\n\n<li>\u2705 You <strong>must select this<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2. <strong>pgAdmin 4<\/strong> (Optional but Recommended)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>web-based GUI (Graphical User Interface)<\/strong> for managing PostgreSQL databases.<\/li>\n\n\n\n<li><strong>Why you might want it<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Makes it easier to create databases, users, and run queries without using the command line.<\/li>\n\n\n\n<li>Very useful if you&#8217;re new to PostgreSQL.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Optional, but <strong>recommended for beginners<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c 3. <strong>Stack Builder<\/strong> (Not required \u274c)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A tool to download and install <strong>additional software or drivers<\/strong>, such as:\n<ul class=\"wp-block-list\">\n<li>PostgreSQL extensions<\/li>\n\n\n\n<li>Foreign data wrappers<\/li>\n\n\n\n<li>ODBC\/JDBC drivers<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why you might want it<\/strong>: If you need extras like PostGIS (for geospatial support), you can install them through Stack Builder.<\/li>\n\n\n\n<li>\u274c <strong>Not necessary for most Django projects<\/strong>. You can skip it.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 4. <strong>Command Line Tools<\/strong> (Essential \u2705)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Includes tools like:\n<ul class=\"wp-block-list\">\n<li><code>psql<\/code>: A command-line interface for interacting with PostgreSQL.<\/li>\n\n\n\n<li>Other utilities for database backup\/restore (<code>pg_dump<\/code>, <code>pg_restore<\/code>, etc.)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Useful for advanced operations, scripting, and debugging.<\/li>\n\n\n\n<li>\u2705 Yes, <strong>recommended and often needed<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Add PostgreSQL to System PATH<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to:\n<ul class=\"wp-block-list\">\n<li><strong>Start \u2192 Environment Variables \u2192 System Variables \u2192 Path<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Add the path to PostgreSQL binaries (e.g., <code>C:\\Program Files\\PostgreSQL\\16\\bin<\/code>)<\/li>\n\n\n\n<li>Click OK and restart your terminal\/command prompt <\/li>\n\n\n\n<li>Run Command prompt as Administrator user<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Create a Database and User in pgAdmin<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open <strong>pgAdmin<\/strong>.<\/li>\n\n\n\n<li>Login with the <strong>postgres<\/strong> user and password.<\/li>\n\n\n\n<li>Right-click <strong>Databases \u2192 Create \u2192 Database<\/strong>\n<ul class=\"wp-block-list\">\n<li>Name: <code>myprojectdb<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Right-click <strong>Login\/Group Roles \u2192 Create \u2192 Role<\/strong>\n<ul class=\"wp-block-list\">\n<li>Name: <code>myprojectuser<\/code><\/li>\n\n\n\n<li>Go to <strong>Definition<\/strong> tab \u2192 Set password.<\/li>\n\n\n\n<li>Go to <strong>Privileges<\/strong> tab \u2192 Enable all privileges.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Go back to the database \u2192 right-click \u2192 <strong>Properties \u2192 Security<\/strong>\n<ul class=\"wp-block-list\">\n<li>Add your <code>myprojectuser<\/code> as the owner (optional).<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Install Python PostgreSQL Adapter<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In your virtual environment or project directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install psycopg2-binary\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Configure Django <code>settings.py<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In your Django project\u2019s <code>settings.py<\/code>, update the <code>DATABASES<\/code> section:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'myprojectdb',\n        'USER': 'myprojectuser',\n        'PASSWORD': 'your_password_here',\n        'HOST': 'localhost',\n        'PORT': '5432',\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 6: Run Migrations<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open <strong>Command Prompt<\/strong> or <strong>PowerShell<\/strong> in your Django project folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py makemigrations\npython manage.py migrate<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 7: Create Superuser <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py createsuperuser<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL must be installed on your system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The PostgreSQL <code>bin<\/code> directory should be added to the <strong>Windows PATH<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Usually: <code>C:\\Program Files\\PostgreSQL\\&lt;version>\\bin<\/code><\/li>\n\n\n\n<li>Confirm by running <code>psql --version<\/code> in Command Prompt.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To Create a PostgreSQL Database via Command Prompt<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Open Command Prompt (as Administrator)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Press <code>Win + R<\/code>, type <code>cmd<\/code>, and press Enter.<\/li>\n\n\n\n<li>Or search &#8220;Command Prompt&#8221; and right-click \u2192 <strong>Run as Administrator<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Switch to PostgreSQL User (Optional)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>postgres<\/code> user was created with a password during installation, you can connect directly with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql -U postgres<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>psql<\/code> is not recognized, check if the PostgreSQL <code>bin<\/code> path is in your system&#8217;s <code>PATH<\/code> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Enter Password (If prompted)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When prompted for a password, enter the one you set during installation.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create a New Database and User<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now you&#8217;re inside the PostgreSQL shell. Run the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Create database\nCREATE DATABASE myprojectdb;\n\n-- Create user with password\nCREATE USER myprojectuser WITH PASSWORD 'mypassword';\n\n-- Grant privileges\nGRANT ALL PRIVILEGES ON DATABASE myprojectdb TO myprojectuser;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To exit the <code>psql<\/code> shell:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\q<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">How to Add PostgreSQL <code>bin<\/code> Directory to Windows PATH<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d Step 1: Find the PostgreSQL <code>bin<\/code> Folder<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, it is usually located at:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Program Files\\PostgreSQL\\&lt;version>\\bin<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example (for version 17):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Program Files\\PostgreSQL\\16\\bin<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>1<\/code>7 with your actual installed version.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Add to System PATH<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">For Windows 10 \/ 11:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Press <strong>Windows + S<\/strong> and search for <strong>Environment Variables<\/strong>.<\/li>\n\n\n\n<li>Click on <strong>&#8220;Edit the system environment variables&#8221;<\/strong>.<\/li>\n\n\n\n<li>In the <strong>System Properties<\/strong> window, click on <strong>Environment Variables<\/strong> (bottom right).<\/li>\n\n\n\n<li>Under <strong>System variables<\/strong>, find and select the variable named <code>Path<\/code>, then click <strong>Edit<\/strong>.<\/li>\n\n\n\n<li>Click <strong>New<\/strong> and paste your PostgreSQL <code>bin<\/code> path, e.g.:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Program Files\\PostgreSQL\\16\\bin<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6. Click <strong>OK<\/strong> on all windows to apply the changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">tep 3: Restart Command Prompt<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Close any open Command Prompt windows.<\/li>\n\n\n\n<li>Open a new Command Prompt.<\/li>\n\n\n\n<li>Type <code>psql --version<\/code> to verify it&#8217;s working.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>psql (PostgreSQL) 17.1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How to Set or Reset the Password for <code>postgres<\/code> User<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use the Query Tool in pgAdmin<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Steps:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open <strong>pgAdmin 4<\/strong>.<\/li>\n\n\n\n<li>Connect to your server.<\/li>\n\n\n\n<li>From the left pane, navigate to:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Servers > PostgreSQL &lt;version> > Databases > postgres<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Right-click on <strong><code>postgres<\/code> (database)<\/strong> and click <strong>Query Tool<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the query window, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER USER postgres WITH PASSWORD 'YourNewSecurePassword';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Click the <strong>Execute\/Run<\/strong> button (lightning icon<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step 1: Install PostgreSQL on Windows Download the installer from: https:\/\/www.postgresql.org\/download\/windows Run the installer and: During the installation of PostgreSQL on Windows, you are asked to select components like: 1.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[48,68],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-postgresql-database","tag-django","tag-postgresql"],"_links":{"self":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=172"}],"version-history":[{"count":0,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}