Connecting to a database

<< Getting help | Firebird Interactive SQL Utility | Creating a database >>

Connecting to a database

A sample database named employee.fdb is located in the examples/empbuild subdirectory of your Firebird installation. Users can use this database to experiment with Firebird. Note that on some POSIX systems, the example database may not be located in the location given above. Each Linux distribution, for example, may have decided to relocate some files.

 SQL> CONNECT database name [user username] [password password] [role role_name];

If any of the the parameters to the CONNECT command contains spaces, you must wrap that parameter in single or double quotes.

If username or password are not supplied, then the current values in the ISC_USER and ISC_PASSWORD environment variables are used instead. There is no environment variable to preset the required role.

It is possible to connect to a database using isql in two ways: locally and remotely.

  • To connect locally, on Windows XP, use the CONNECT statement with the full file path or an alias (for a local database):
 SQL> CONNECT "C:\DATABASES\FIREBIRD\MY_EMPLOYEE.FDB"

On Linux, a similar example would be:

 SQL> CONNECT "/databases/firebird/MY_EMPLOYEE.FDB"
  • If connecting remotely (using TCP/IP), use the CONNECT statement with the server name and complete file path of the database or an alias. When using the full path, remember to ensure that the server name is separated from the database path with a colon.

To connect to a database on a Linux/UNIX server named cosmos:

 SQL> CONNECT 'cosmos:/usr/firebird/examples/employee.gdb';

To connect to a database on a Windows server named cosmos:

 SQL> CONNECT 'cosmos:C:\DATABASES\FIREBIRD\MY_EMPLOYEE.FDB'

Note: Firebird is slash agnostic and automatically converts either type of slash to suit the relevant operating system.

Using database alias names

In the examples above, we have been using the full path to the database file. This has a disadvantage in that all clients will be able to determine exactly where the database is to be found, or, may cause problems when the database has to be moved. To alleviate these problems, database aliases can be used.

Once Firebird has been installed, a file named aliases.conf can be found in the main installation folder. By adding an entry to this folder, the full path to the database can be simplified to an alias name. This makes connecting easier, hides the actual database path from inquisitive users and allows the database to be moved around as necessary without having to change all the clients to allow them to connect to the database at the new location.

To create an alias for the database currently known as /databases/firebird/MY_EMPLOYEE.FDB on the cosmos Linux server, we need to add the following to the aliases.conf file on the cosmos server. Bydefault, this will be in the folder /opt/firebird. On Linux, this file is owned by the root user and so, must be updated by the root user. On Windows, you need to be either an administrator, a power user or SYSTEM to change the file.

 my_employee = /databases/firebird/MY_EMPLOYEE.FDB

There should be no quotes around the path to the database file.

Regardless of where the database file is currently located, or if it has its physical file name renamed, etc, all the local users will refer to the database simply as my_employee. Remote users will refer to this database as cosmos:my_employee. The following example shows an isql session being connected locally to the database using the alias name rather than a full path:

 cosmos> /opt/firebird/bin/isql my_employee
 Database:   test, User: sysdba

 SQL>

Alternatively, a remote connection would be made as follows, specifying the server name and the database alias together:

 C:\Program Files\Firebird\Firebird_2_0\bin>isql cosmos:my_employee
 Database:   cosmos:my_employee

 SQL>

Because the alias is defined on the server where the database resides, then the remote client needs to supply the server name and the alias name (as defined on that server) in order to make a connection.

Using the CONNECT command in an existing isql session is equally as simple using alias names:

 SQL> CONNECT 'cosmos:my_employee';
 Database:   cosmos:my_employee

 SQL>

Caution: Regarding the security aspect of using database alias names to hide the full path to the actual database file(s), it's not really all that secure as the following SQL command shows:

 SQL> select MON$DATABASE_NAME from mon$database;

 MON$DATABASE_NAME
 =================================
 /data/databases/firebird/test.fdb

See also:
Connect to an existing Database: IBExpert Database menu item

back to top of page
<< Getting help | Firebird Interactive SQL Utility | Creating a database >>