I need to connect to some PostgreSQL server providing some credentials, and print a list of available databases on that host for a given user.
I am trying:
<?php $connection = pg_connect("host=localhost user=testuser password=123 connect_timeout=5"); ?>
And I get:
Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: database "testuser" does not exist in /var/www/test.php on line 56
I thought this must be possible because phpPgAdmin does it, but I looked at phpPpAdmin sources and found that they connect to a database named template1
.
From http://www.postgresql.org/docs/8.1/interactive/manage-ag-templatedbs.html:
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/pgSQL in template1, it will automatically be available in user databases without any extra action being taken when those databases are made.
Is there a way to connect without specifying any database?
Step 1: Log in to the server using the SQL Shell (psql) app. Step 2: Run the following query: SELECT datname FROM pg_database; psql runs the query against the server and displays a list of existing databases in the output.
The default username for postgres is postgres. (If you are using Advanced Server it is enterprisedb.) On a Mac or Windows, you are able to connect to the default instance by simply hitting enter at the shell or command prompt when trying to run psql and keying in the password.
The following steps show you how to connect to the PostgreSQL database server via the psql program: First, launch the psql program and connect to the PostgreSQL Database Server using the postgres user: Second, enter all the information such as Server, Database, Port, Username, and Password.
use sudo -i -u postgres command to connect with the postgres user in PostgreSQL. type psql to start the PostgreSQL terminal, once this window is open we can start typing queries. Next step in the process is to create a database. We have created a database with the name ‘ users ‘.
Type psql on the terminal, you will see postgres=#, Now you can start typing the queries. You can not to the database directly by passing psql command after the user account. In the below syntax, Postgres is the name of the user account. The first query we are going to pass is to create a new database and then we’ll connect to that database.
Postgres by default has 3 databases, out of them one is with the name ‘Postgres’, and the user account is also created with the same name. First, we’ll show you how to connect with a user account in PostgreSQL.
You have to connect to a database. Which database you could use for a "maintenance database" depends on the installation and the subsequent administration. After a default installation there are 2 databases that could be used for the initial connection - "template1" and "postgres". It's wise to create a new user and a database with the same name and use those.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With